Ilmedin zeeshan
Ilmedin zeeshan

Reputation: 57

for loop not removing all items from list,what is the solution?

I know why this loop not deleting all the items from list1 with the help of other answers to same question, my question here is what change to made in loop code to remove all the items from list1.

Here is my code:

    list1 = [1,'one',2,'two',3,'three']
    for x in list1:
      if x in list1:
        list1.remove(x)
    print(list1)

Thanks in advance.

Upvotes: 2

Views: 1378

Answers (6)

Mahesh
Mahesh

Reputation: 1

L = [1,2,3,4,5]

for i in L:

    L.remove(i)

print(L)

In the above code, we will expect an empty list but it will display the following output [2,4]. Let me explain what happens at each iteration to make it easier to understand.

In the 1st iteration, element 1 is removed which is at the 0th index i.e., L[0].

In the second iteration, the length of the list changes, and the elements move to the left i.e., L is now [2,3,4,5]. Again, indices in Python start with 0, so L[0] = 2, L[1] = 3, etc. Now the variable i is L[1] in the updated list, so L[1] = 3 is removed and this process will continue until the end of the list.

The solution is, as others suggested, del L[:].

Upvotes: 0

abhi krishnan
abhi krishnan

Reputation: 836

for deleting the value from an existing list: first, take the length of the list

list1 = [1,'one',2,'two',3,'three']
for i in range(len(list1)):
    list1.pop()

it will remove the last item , each time the loop execution is in progress

             OR

you can do it like

list1 = [1,'one',2,'two',3,'three']
for i in range(len(list1)):
     del list1[0]

Upvotes: 0

Rahul Singh Raghav
Rahul Singh Raghav

Reputation: 103

The reason for why you are not able to delete all elements is because when you do in in list for loop it starts looping with first element, but when you delete that element and move to next element you actually move to third element by skipping second element. Let me take the example list1 = [1, "one", 2, "two", 3, "three"] In first iteration, Current element is 1 and after completion of first iteration, you delete 1 and current element is one and then you move to next element which is actually 2 and that's how you miss to remove one and same continues to happen and at last you are left with "one", "two", "three".

I know bit confusing but i guess it helps.

Upvotes: 1

RohithS98
RohithS98

Reputation: 510

You could just do this:

list1 = []

However if you want to delete each element then:

while list1!=[]:
    del list1[0]

EDIT: Much better and faster method as suggested by Yann Vernier:

del list1[:]

Upvotes: 3

Taohidul Islam
Taohidul Islam

Reputation: 5414

If you want to delete all list items using loop then I think this is one of the shortest ones:

while list1:
    del(list1[0]) #Before deletion of all items,always there will be an item at index 0

Also this trick might help you!

list1 = [1,'one',2,'two',3,'three']
for i in range(len(list1)):
    del (list1[0]) #Before deletion of all items,always there will be an item at index 0
print(list1)

Upvotes: 0

Mike Scotty
Mike Scotty

Reputation: 10782

Don't change objects while you're iterating them (unless ofc you know what you're doing).

A possible work-around would be to iterate a copy and modify the original object:

list1 = [1,'one',2,'two',3,'three']
for x in list1[:]:
  if x in list1:
    list1.remove(x)
print(list1)

Output:

[]

Upvotes: 0

Related Questions