Take the nested lists:
list = [[foo, foo], [foo, foo]], [foo, foo]]
.
I pass list
to a function, but I also separately pass by reference to the same function the second nested list as list[1]
.
Then, within the function at random indices, I add 3 more [foo, foo]
's to list
.
Would it be possible to identify which [foo, foo]
in list
is the one I passed to the function?
In other words, I want to get the new index of the original [foo, foo]
I separately passed earlier in the now-modified list
.
What you are looking for is is
, which tests if two variables refer to the same object.
Demonstrating in iPython shell:
In [1]: list = [['foo', 'foo'], ['foo', 'foo'], ['foo', 'foo']]
In [2]: sublist = list[1]
In [3]: sublist is list[1]
Out[3]: True
In [5]: list.insert(1, ['foo', 'foo'])
In [6]: list
Out[6]: [['foo', 'foo'], ['foo', 'foo'], ['foo', 'foo'], ['foo', 'foo']]
In [7]: list.insert(1, ['foo', 'foo'])
In [8]: list
Out[8]:
[['foo', 'foo'],
['foo', 'foo'],
['foo', 'foo'],
['foo', 'foo'],
['foo', 'foo']]
In [9]: sublist is list [1]
Out[9]: False
In [10]: for count, l in enumerate(list):
...: if l is sublist:
...: print(count)
...:
3
In [11]: sublist is list[3]
Out[11]: True