0

Let's say I have:

a = 1
b = 2
C = 'r'

my_list = [a,b,c]

Now let's say that a, b and c are unknown and I don't know their names.

If I do:

for x in my_list: del x

it doesn't work. a, b, c have not been deleted.

Can someone explain me why?

user9187374
  • 299
  • 1
  • 3
  • 11
  • 2
    variables != objects in python, deleting a variable only decrements its reference count in memory :) – cs95 Feb 15 '18 at 13:03
  • are you trying to delete the entire list? – CodeCupboard Feb 15 '18 at 13:03
  • `x` is re-assigned to the next value of `my_list` at the next iteration of the loop, what are you actually trying to do? – Chris_Rands Feb 15 '18 at 13:05
  • Possible duplicate of [Difference between del, remove and pop on lists](https://stackoverflow.com/questions/11520492/difference-between-del-remove-and-pop-on-lists) – WSMathias9 Feb 15 '18 at 13:22
  • I don't want to just delete every element from list, infact I don't do del my_list[x]. I would like to delete the objects but having only a list of them. – user9187374 Feb 15 '18 at 13:29

2 Answers2

1

As @Coldspeed mentions in his comment, the variable x which you delete is not the same as the element in the list object.

Similar behaviour will be seen if you try to assign to x:

for x in my_list: x='bla' #does not modify anything in my_list

However, as the items are references to the same memory block, the comparison x is my_list[0] will equate to True in the first loop iteration.

As such, it is possible to perform operations on the list through usage of the shared reference, for example:

for x in my_list[:]: my_list.remove(x) #results in an empty list

Care has to be taken to first create a copy of the list and iterate over these items though, as was done in the previous lines. If you are hasty and loop over the items of a dynamically changing list, you will run into some more python magic.

for x in my_list: my_list.remove(x) #the first element gets deleted, then the second element in the list, which now has length 2, is deleted.
#Final result is the list [2] remaining
Uvar
  • 3,372
  • 12
  • 25
1

you have multiple issues here:

1. variable in list

a = 1
b = 2
my_list = [a,b]

assigns the values 1 and 2 to the list, not the vars. You can use mutable objects to get you desire: Immutable vs Mutable types

2. deleting a copy from a listvalue

for x in my_list:
    del x

like in 1. x is just the value from the list (e.g. 1, 2, 'c'), but even worse, its a additional reference count to the memory.
Deleting it results in decreasing the counter, not deleting the value from memory, since at least one more counter is given by the original list (and in your case the vars (a,b,c) from the beginning).

More Info: Arguments are passed by assignment

3. deleting while iterating

for x in my_list:
    del x

contains an other problem. If you would change the code to mylist.remove(x), to at least remove the entrie from the list, you would also skip every second member of the list. Quick Example:

li = [1,2,3]
for x in li: 
    li.remove(x)

first iteration would be x = 1. Deleting 1 from li results in li = [2,3]. Then the loop continous with the second position in the list: x=3 and deleting it. 2 was skipped.

This can be avoided by using a copy of the list using the [:] operator:

for x in li[:]: 
    li.remove(x)

This finaly results in an empty list

Skandix
  • 1,916
  • 6
  • 27
  • 36