I was wondering if it was possible to get a "pointer" to an element in a python list. That way, I would be able to access my element directly without needing to know my element's index. What I mean by that is that in a list, you can add elements anywhere; at the start, in the middle or even at the end, yet the individual elements aren't moved from their actual memory location. In theory, it should be possible to do something like:
myList = [1]
[1]
element = &myList[0]
element would act as a pointer here.
myList.insert(0, 0)
myList.append(2)
[0, 1, 2]
At this point, I would still be able to access the element directly even though it's index within the list has changed.
The reason I want to do this is because in my program, it would be way too tedious to keep track of every item I add to my list. Each item is generated by an object. Once in a while, the object has to update the value, yet it can't be guaranteed that it will find its item at the same index as when it was added. Having a pointer would solve the problem. I hope that makes sense.
What would be the right way to do something like that in Python?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…