How to copy one segment from a segmentation node to a different segmentation with code?

I’m trying to copy a segment from one segmentation to another, but I don’t know how to do it automatically in my code. I know that we can drag and drop in the data module, but is there a way to do this with code?

A workaround could be to export all the segments of Segmentation1 as LabelMap and import it to Segmentation2, then delete all the undesired segments from Segmentation2

yes, that would work but I just wanted to know if I could avoid doing that and just move one segment.

Yes, you can copy/move segments between segmentations. You can track down how it is implemented in Slicer core and replicate that in your script.