It's not clear what you need, so please edit the original question to let us know what output you need. But, some suggestions...
The first issue is; you don't have two lists. You have a
and b
, both of which are a nested list of nested lists (3 levels deep) for a total of 14 lists involved.
a = [ # First level list "a"
[ # Second level list I.e. a[0]
[] # Third level list a[0][0]
],
[ # Second list I.e. a[1]
[0.4, 2] # Third level list a[1][0] = 0.4, a[1][1] = 2
],
[ # Second level list I.e. a[2]
[0.8, 1] # Third level list a[2][0] = 0.8, a[2][1] = 1
]
]
So you need to decide where you want things reversed.
The second issue is the idea of a reference
versus a copy
in Python. A good discussion starts here. But, simply put, a variable name is a reference to an object - not the object itself.
So, when you create a
with ...
a = [[[]], [[0.4, 2]], [[0.8, 1]]]
...you create a list object in memory, with the variable reference a
. That list object contains references to three other list objects which have been created in memory (a[0]
, a[1]
, a[3]
), each one of which contains a reference to one additional list object (a[0][0]
, a[1][0]
, a[2][0]
).
If you you assign a
to A
with ...
a = [[[]], [[0.4, 2]], [[0.8, 1]]]
print ("the memory location for object 'a' is:", hex(id(a)))
A = a
print ("the memory location for object 'A' is:", hex(id(A)), "(The same location)")
b = [i for i in reversed(a)]
print ("the memory location for object 'b' is:", hex(id(b)), "(A different location)")
... a
and A
are the same object, but b
is a new object. So print(a is b)
returns False
.
I.e. ...
the memory location for object 'a' is: 0x7fdc65b12308
the memory location for object 'A' is: 0x7fdc65b12308 (The same location)
the memory location for object 'b' is: 0x7fdc65b126c8 (A different location)
HOWEVER, as pointed out by @Derte Trdelnik
above, when you created b, you only copied the reference for the sublists - NOT the object. I.e.
a = [[[]], [[0.4, 2]], [[0.8, 1]]]
b = [i for i in reversed(a)]
print ("the memory location for object 'a' is:", hex(id(a)))
print ("the memory location for object 'b' is:", hex(id(b)), "(A different location)")
print ("the memory location for sub-list object 'a[1]' is:", hex(id(a[1])) )
print ("the memory location for sub-list object 'b[1]' is:", hex(id(b[1])), "(The same location as a[1])" )
OUTPUT:
the memory location for object 'a' is: 0x7f49b46f59c8
the memory location for object 'b' is: 0x7f49b46f5a08 (A different location)
the memory location for sub-list object 'a[1]' is: 0x7f49b46f5908
the memory location for sub-list object 'b[1]' is: 0x7f49b46f5908 (The same location as a[1])