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
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:")
Size_of_List = len(List_of_Users)
print(f"The List contains {Size_of_List} members")
for User in List_of_Users:
    print(f"The new List: {List_of_Users} ")
    Size_of_List = len(List_of_Users)
    print(f"The List contains {Size_of_List} members")
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.
You should not alter a list you are iterating over, instead iterate a copy and alter the original.
for User in List_of_Users:
for User in List_of_Users[:]:
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
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):

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

print(f"\nFinal length: {len(list_of_users)}")
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

Possibly Related Threads…
Thread Author Replies Views Last Post
  Splitting strings in list of strings jesse68 3 1,137 Mar-02-2022, 05:15 PM
Last Post: DeaD_EyE
  Convert each element of a list to a string for processing tester_V 6 3,566 Jun-16-2021, 02:11 AM
Last Post: tester_V
  Greedy algorithms on logical problems Opensourcehacker 0 1,160 Nov-22-2020, 05:12 PM
Last Post: Opensourcehacker
  Strange syntax error with f-strings Askic 6 2,698 Oct-16-2020, 10:40 AM
Last Post: Askic
  Unable to bit shift and logical OR bytes and ints? MysticLord 7 4,561 Sep-01-2020, 03:31 PM
Last Post: deanhystad
  Basic logical errors cpopescu 3 1,490 Jun-03-2020, 11:30 AM
Last Post: menator01
  How to find the first and last of one of several characters in a list of strings? tadsss 2 1,656 Jun-02-2020, 05:23 PM
Last Post: bowlofred
  Python logical operator AND rasec70 4 1,870 May-07-2020, 03:40 PM
Last Post: pyzyx3qwerty
  Iterating through a list of strings Ash_Ren 1 1,581 Nov-22-2019, 08:30 PM
Last Post: buran
  How do I delete symbols in a list of strings? Than999 1 1,799 Nov-16-2019, 09:37 PM
Last Post: ibreeden

Forum Jump:

User Panel Messages

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