Python Forum
del in for loop not deleting everything
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
del in for loop not deleting everything
#1
My problem is that I want to use del in a for loop to delete everything in a list.

Code:
users = ['a', 'b', 'c', 'd', 'e']

for user in users:
    del users[0]
 
print(users)
However, this deletes everything except the last 2 values.
I am a beginner and learning. Please explain why this is happening. Thank you Smile
Reply
#2
Add print(user, users) just before line 4 to see what happens.
noswad likes this post
Reply
#3
users = ['0', '1', '2', '3', '4']

print('for user in users:')
for user in users:
    print('delete ', users[0], 'from', users, 'user =', user)
    del users[0]
print('users =', users)

users = ['0', '1', '2', '3', '4']
print('\nwhile i < len(users):')
i = 0
while i < len(users):
    print('delete ', users[0], 'from', users, 'i =', i)
    del users[0]
    i += 1
print('users =', users)

users = ['0', '1', '2', '3', '4']
print('\nwhile len(users) > 0:')
while len(users) > 0:
    print('delete ', users[0], 'from', users)
    del users[0]
print('users =', users)
Output:
for user in users: delete 0 from ['0', '1', '2', '3', '4'] user = 0 delete 1 from ['1', '2', '3', '4'] user = 2 delete 2 from ['2', '3', '4'] user = 4 users = ['3', '4'] while i < len(users): delete 0 from ['0', '1', '2', '3', '4'] i = 0 delete 1 from ['1', '2', '3', '4'] i = 1 delete 2 from ['2', '3', '4'] i = 2 users = ['3', '4'] while len(users) > 0: delete 0 from ['0', '1', '2', '3', '4'] delete 1 from ['1', '2', '3', '4'] delete 2 from ['2', '3', '4'] delete 3 from ['3', '4'] delete 4 from ['4'] users = []
Reply
#4
(Nov-22-2020, 08:52 PM)Gribouillis Wrote: Add print(user, users) just before line 4 to see what happens.

Okay, I see what is happening now, but why is it skipping values? Shouldn't the first value always be deleted because it is [0]?
Reply
#5
(Nov-22-2020, 09:04 PM)deanhystad Wrote:
users = ['0', '1', '2', '3', '4']

print('for user in users:')
for user in users:
    print('delete ', users[0], 'from', users, 'user =', user)
    del users[0]
print('users =', users)

users = ['0', '1', '2', '3', '4']
print('\nwhile i < len(users):')
i = 0
while i < len(users):
    print('delete ', users[0], 'from', users, 'i =', i)
    del users[0]
    i += 1
print('users =', users)

users = ['0', '1', '2', '3', '4']
print('\nwhile len(users) > 0:')
while len(users) > 0:
    print('delete ', users[0], 'from', users)
    del users[0]
print('users =', users)
Output:
for user in users: delete 0 from ['0', '1', '2', '3', '4'] user = 0 delete 1 from ['1', '2', '3', '4'] user = 2 delete 2 from ['2', '3', '4'] user = 4 users = ['3', '4'] while i < len(users): delete 0 from ['0', '1', '2', '3', '4'] i = 0 delete 1 from ['1', '2', '3', '4'] i = 1 delete 2 from ['2', '3', '4'] i = 2 users = ['3', '4'] while len(users) > 0: delete 0 from ['0', '1', '2', '3', '4'] delete 1 from ['1', '2', '3', '4'] delete 2 from ['2', '3', '4'] delete 3 from ['3', '4'] delete 4 from ['4'] users = []

Could you please elaborate?
Reply
#6
It would be better if you figure it out yourself. I suggest studying the "while" loop version. Pay close attention to how the index (i) changes, how the len(users) changes, and why this stops the loop after removing only three users.

The for loop has the same problem as the while loop. The only difference is the for loop does not expose the index it is using to increment through the loop.

Here's another way to delete all the users:
users = ['0', '1', '2', '3', '4']
print('\nfor i in range(len(users)):')
for i in range(len(users)):
    print('delete ', users[0], 'from', users, 'i =', i)
    del users[0]
print('users =', users)
Output:
for i in range(len(users)): delete 0 from ['0', '1', '2', '3', '4'] i = 0 delete 1 from ['1', '2', '3', '4'] i = 1 delete 2 from ['2', '3', '4'] i = 2 delete 3 from ['3', '4'] i = 3 delete 4 from ['4'] i = 4
From the output it is obvious that len(users) is only evaluated once.

And it should be obvious by now why this doesn't work. It is very similar to the previous version with one critical difference.
users = ['0', '1', '2', '3', '4']
print('\nfor i in range(len(users)):')
for i in range(len(users)):
    print('delete ', users[i], 'from', users, 'i =', i)
    del users[i]
print('users =', users)
Output:
for i in range(len(users)): delete 0 from ['0', '1', '2', '3', '4'] i = 0 delete 2 from ['1', '2', '3', '4'] i = 1 delete 4 from ['1', '3', '4'] i = 2 Traceback (most recent call last): File "C:\Users\djhys\Documents\python\musings\junk.py", line 28, in <module> print('delete ', users[i], 'from', users, 'i =', i) IndexError: list index out of range
Reply
#7
noswad Wrote:Okay, I see what is happening now, but why is it skipping values?
The iterator used in the 'for' loop moves from left to right in the list by increasing an internal index. As you remove the first item, the elements in the list move from right to left. So every time the iterator makes one step to the right, the list elements make one step to the left. That's the reason why it is skipping values. Then it stops because it reaches the end of the list, before the list is empty.
Reply
#8
(Nov-22-2020, 10:06 PM)Gribouillis Wrote:
noswad Wrote:Okay, I see what is happening now, but why is it skipping values?
The iterator used in the 'for' loop moves from left to right in the list by increasing an internal index. As you remove the first item, the elements in the list move from right to left. So every time the iterator makes one step to the right, the list elements make one step to the left. That's the reason why it is skipping values. Then it stops because it reaches the end of the list, before the list is empty.

Oh, okay. I see now! Thank you for taking the time to respond to my thread! Smile
Reply
#9
(Nov-22-2020, 09:42 PM)deanhystad Wrote: It would be better if you figure it out yourself. I suggest studying the "while" loop version. Pay close attention to how the index (i) changes, how the len(users) changes, and why this stops the loop after removing only three users.

The for loop has the same problem as the while loop. The only difference is the for loop does not expose the index it is using to increment through the loop.

Here's another way to delete all the users:
users = ['0', '1', '2', '3', '4']
print('\nfor i in range(len(users)):')
for i in range(len(users)):
    print('delete ', users[0], 'from', users, 'i =', i)
    del users[0]
print('users =', users)
Output:
for i in range(len(users)): delete 0 from ['0', '1', '2', '3', '4'] i = 0 delete 1 from ['1', '2', '3', '4'] i = 1 delete 2 from ['2', '3', '4'] i = 2 delete 3 from ['3', '4'] i = 3 delete 4 from ['4'] i = 4
From the output it is obvious that len(users) is only evaluated once.

And it should be obvious by now why this doesn't work. It is very similar to the previous version with one critical difference.
users = ['0', '1', '2', '3', '4']
print('\nfor i in range(len(users)):')
for i in range(len(users)):
    print('delete ', users[i], 'from', users, 'i =', i)
    del users[i]
print('users =', users)
Output:
for i in range(len(users)): delete 0 from ['0', '1', '2', '3', '4'] i = 0 delete 2 from ['1', '2', '3', '4'] i = 1 delete 4 from ['1', '3', '4'] i = 2 Traceback (most recent call last): File "C:\Users\djhys\Documents\python\musings\junk.py", line 28, in <module> print('delete ', users[i], 'from', users, 'i =', i) IndexError: list index out of range

Okay, I wasn't understanding because I haven't learned about while loops, yet. I am going to learn about the soon, though. When I do, I will come back to review this code. Thank you for taking the time to respond to my thread! Smile
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020