0

What would be the best way to transfer references of objects from one list to another (move objects from one list to another). For clarity, I need to remove the objects from d[1] after copying

class MyObject:
    def __init__(self,v):
        self.value = v

d = {1: [MyObject("obj1"),MyObject("obj2")], 2: []}

#which one?
#d[2] = [obj for obj in d[1]]

#d[2] = d[1][:]

#d[2] = d[1].copy()

#clear d[1]
#d[1] = []

for i in range(len(d[1])):
    d[2].append(d[1].pop(0))

for o in d[2]:
    print (o.value)
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
ragardner
  • 1,836
  • 5
  • 22
  • 45
  • *"best"* how, exactly? Does it matter whether or not it's destructive of the previous list (some of your suggestions are, some aren't). And why not just pick one and spend your time on something else more useful? – jonrsharpe Jun 28 '17 at 20:22
  • Possible duplicate of [How to clone or copy a list?](https://stackoverflow.com/questions/2612802/how-to-clone-or-copy-a-list) – victor Jun 28 '17 at 20:23
  • Which *"people"*? Why not ask *them* which they'd prefer? Or use [codereview.se] once you have something more practical together? – jonrsharpe Jun 28 '17 at 20:25
  • 1
    You certainly need to provide *context*. To decide which is better you need to specify *criteria*, otherwise it's just an opinion. If it's just *"which one would [random SO user] prefer"*, then who can say but them? – jonrsharpe Jun 28 '17 at 20:33

2 Answers2

3

Which approach is best depends a bit on the details of the surrounding code. Does any other variable or data structure contain a reference to either of your lists? If not, you can just rebind the references in the dict (which takes O(1) time since no copying happens):

d = {1: [MyObject("obj1"),MyObject("obj2")], 2: []}
d[2] = d[1]
d[1] = []

If other references to the existing lists might exist and you want them to continue referring to the correct values (e.g. an old reference to d[1] should still reference d[1] after the changes), then you want to do a slice assignment followed by a clear (this is O(N)):

d[2][:] = d[1] # copy data
d[1].clear()

I don't think there's a good reason to use any other approach unless you have some other logic to apply (for instance, if you only want to copy some of the values and not others).

Blckknght
  • 100,903
  • 11
  • 120
  • 169
1

Adding a dictionary obscures the use case. Based on your examples, it's not clear if you want a copy of the object or a list referencing the same objects.

Assuming the latter, consider the simplified case. It's really as simple as assigning the list to another variable:

>>> class MyObject(object):
...     def __init__(self, v):
...         self.value = v
...
>>> x = [MyObject(1), MyObject(2)]
>>> y = x
>>> x[1].value
2

Now, both x and y are a list of the same referenced objects. If I change the object in one list, it will change in the other:

>>> y[1].value = 3
>>> x[1].value
3

In your use case (a dictionary with list values), this is quite simple:

d[2] = d[1]

You can then delete the 1 key if necessary:

del d[1]

Voila!

brianpck
  • 8,084
  • 1
  • 22
  • 33