Python Forum
Help with Logical error processing List of strings
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with Logical error processing List of strings
#1
This code is to remove one item from a List of "names" until the List is empty using for loop. The program does not complete the List stopping before the list is empty. See output 5 items on original List. Only 3 items removed
# Simple Test to empty List using loop
List_of_Users = ['josh', 'admin', 'cookie','jerry','jack']   
#
print("Starting List Below:")
print(List_of_Users)
Size_of_List = len(List_of_Users)
print(f"The List contains {Size_of_List} members")
for User in List_of_Users:
    List_of_Users.pop(0)
    print(f"The new List: {List_of_Users} ")
    Size_of_List = len(List_of_Users)
    print(f"The List contains {Size_of_List} members")
    print("")
Output:
Output:
Starting List Below: ['josh', 'admin', 'cookie', 'jerry', 'jack'] The List contains 5 members The new List: ['admin', 'cookie', 'jerry', 'jack'] The List contains 4 members The new List: ['cookie', 'jerry', 'jack'] The List contains 3 members The new List: ['jerry', 'jack'] The List contains 2 members [Finished in 254ms]
Yoriz write Nov-26-2022, 04:27 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
You should not alter a list you are iterating over, instead iterate a copy and alter the original.
Change
for User in List_of_Users:
To
for User in List_of_Users[:]:
Reply
#3
Ask yourself if you need to remove items from a list. I never do this. I build a new list that only contains the items I want to keep.

If you think you need to remove all items, for is the wrong choice for your loop.
while list_of_users:
    user = list_of_users.pop(0)
    # whatever
Reply
#4
You can alter a list in place so long as you do it in reverse order:
list_of_users = ['josh', 'admin', 'cookie','jerry','jack']

def pop_item(lname):
    if len(lname):
        lname.pop(len(lname)-1)

while len(list_of_users):
    print(f"List length: {len(list_of_users)}, {list_of_users}")
    pop_item((list_of_users))

print(f"\nFinal length: {len(list_of_users)}")
results:
Output:
List length: 5, ['josh', 'admin', 'cookie', 'jerry', 'jack'] List length: 4, ['josh', 'admin', 'cookie', 'jerry'] List length: 3, ['josh', 'admin', 'cookie'] List length: 2, ['josh', 'admin'] List length: 1, ['josh'] Final length: 0
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to read module/class from list of strings? popular_dog 1 424 Oct-04-2023, 03:08 PM
Last Post: deanhystad
  Trying to understand strings and lists of strings Konstantin23 2 699 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  problem in using int() with a list of strings akbarza 4 647 Jul-19-2023, 06:46 PM
Last Post: deanhystad
  Delete strings from a list to create a new only number list Dvdscot 8 1,466 May-01-2023, 09:06 PM
Last Post: deanhystad
  Splitting strings in list of strings jesse68 3 1,703 Mar-02-2022, 05:15 PM
Last Post: DeaD_EyE
  Convert each element of a list to a string for processing tester_V 6 5,170 Jun-16-2021, 02:11 AM
Last Post: tester_V
  Greedy algorithms on logical problems Opensourcehacker 0 1,507 Nov-22-2020, 05:12 PM
Last Post: Opensourcehacker
  Strange syntax error with f-strings Askic 6 4,076 Oct-16-2020, 10:40 AM
Last Post: Askic
  Unable to bit shift and logical OR bytes and ints? MysticLord 7 6,817 Sep-01-2020, 03:31 PM
Last Post: deanhystad
  Basic logical errors cpopescu 3 2,011 Jun-03-2020, 11:30 AM
Last Post: menator01

Forum Jump:

User Panel Messages

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