Feb-21-2025, 05:59 AM
Internally, Python lists are integer indexed dynamic arrays. Here is an interesting, if old, question and interesting answers and links on stackoverflow.
There is quite a lot going on behind the scenes when you add or delete elements!
As I understand it, you cannot access the values in a list without using the pointer variables, the index, which tells us where they are in memory and what kind of value is stored there.
So, if my_list.remove(3) seems to get rid of 3 without using its index, that is only superficially so.
I don't know how .remove() works internally, but you can fake it like this:
There is quite a lot going on behind the scenes when you add or delete elements!
As I understand it, you cannot access the values in a list without using the pointer variables, the index, which tells us where they are in memory and what kind of value is stored there.
So, if my_list.remove(3) seems to get rid of 3 without using its index, that is only superficially so.
I don't know how .remove() works internally, but you can fake it like this:
def remove_it(val, alist): for i in range(len(alist)): if alist[i] == val: alist.pop(i) return alist my_list = [1, 2, 3, 4, 5] remove_it(3, my_list)
Output:[1, 2, 4, 5]
Or like this:def remove_it(val, alist): for i in range(len(alist)): if alist[i] == val: del alist[i] return alist my_list = [1, 2, 3, 4, 5] remove_it(3, my_list)
Output:[1, 2, 4, 5]