Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
For List Loop Issue
#1
Hello, good people of the Python forums! I am new to coding and Python both, it's nice to meet you all!

I am writing a simple program that compares a new user list against a current user list. After I made a copy of the original current user list I want to delete all of the values in it. I originally made a for loop to remove the items in the list but for some reason, it's skipping every other value. So it will remove jesse, bob, and justin. But alecs and Jameson will remain.

I have fixed it and instead have put in 'current_users.clear()' to clear out the list. Simpler and easier.

What I am having trouble with is understanding why the for loop is skipping over the items it does and was hoping to get some insight.

current_users = ['jesse', 'alecs', 'bob', 'Jameson', 'justin']

for current in current_users:
	print(current)
	current_users.remove(current)

print(current_users)
Reply
#2
It goes through the loop and deletes the first entry. Then the second entry becomes the first. Then it goes through the loop looking for the second entry, but the whole list is shifted over from when you removed the first entry.
Reply
#3
(Dec-31-2019, 04:37 AM)Galdain Wrote: Hello, good people of the Python forums! I am new to coding and Python both, it's nice to meet you all!

I am writing a simple program that compares a new user list against a current user list. After I made a copy of the original current user list I want to delete all of the values in it. I originally made a for loop to remove the items in the list but for some reason, it's skipping every other value. So it will remove jesse, bob, and justin. But alecs and Jameson will remain.

I have fixed it and instead have put in 'current_users.clear()' to clear out the list. Simpler and easier.

What I am having trouble with is understanding why the for loop is skipping over the items it does and was hoping to get some insight.

current_users = ['jesse', 'alecs', 'bob', 'Jameson', 'justin']

for current in current_users:
	print(current)
	current_users.remove(current)

print(current_users)

Ahhhh, that makes sense now. I feel special, thank you very much!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  List Comprehension Issue johnywhy 5 537 Jan-14-2024, 07:58 AM
Last Post: Pedroski55
  Python List Issue Aggie64 5 1,617 Jun-30-2022, 09:15 PM
Last Post: Aggie64
  List to table issue robdineen 2 1,464 Nov-07-2021, 09:31 PM
Last Post: robdineen
  Calculator code issue using list kirt6405 4 2,280 Jun-11-2021, 10:13 PM
Last Post: topfox
  Appending to list of list in For loop nico_mnbl 2 2,366 Sep-25-2020, 04:09 PM
Last Post: nico_mnbl
  Issue accessing data from Dictionary/List in the right format LuisSatch 2 2,211 Jul-25-2020, 06:12 AM
Last Post: LuisSatch
  Append list into list within a for loop rama27 2 2,384 Jul-21-2020, 04:49 AM
Last Post: deanhystad
  IndexError: List index out of range issue Adem 1 3,535 Nov-01-2019, 10:47 PM
Last Post: ichabod801
  List/String seperation issue YoungGrassHopper 13 5,485 Sep-20-2019, 11:57 AM
Last Post: perfringo
  List Issue Batman 3 2,647 Jun-06-2019, 11:56 PM
Last Post: Batman

Forum Jump:

User Panel Messages

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