Python Forum
Removing items in a list - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Removing items in a list (/thread-30698.html)



Removing items in a list - cap510 - Nov-01-2020

I have tried removing multiple items from a list, but the loop ends without the ability to continue. I am to create a list of names and then use a loop to remove more than one name.

names = []
while True:
    first = input("Enter first name or q to quit: ")
    if first.lower() == "q":
        break
    names.append(first)
print()
namesL = names[:]
while True:
    x_name = input("Enter a name to remove or q to quit: ")
    namesL.remove(x_name)
    names.append(namesL)
    if first.lower() == "q":
        break
    else:
        continue

print(namesL)
The second while loop ends without prompting for more entries. I have tried to append list, but still does not work.I am trying to ask user to enter names and remove them from list until they enter q to quit.
[/python]


RE: Removing items in a list - GOTO10 - Nov-01-2020

On line 13 you are checking the value of first.lower(). Look at your logic again and make sure that is the variable you want to check at that point. Also, consider what happens on line 11 if a user entered "q" on line 10.


RE: Removing items in a list - Larz60+ - Nov-01-2020

you don't need lines 15 and 16

look at these two lines, what's wrong here?:
    namesL.remove(x_name)
    names.append(namesL)
first you are deleting from namesL.
OK, that entry is gone. But now you are trying to add it back to the original list 'name'.

Also first was set to 'q' in forst loop, so is still equal to 'q' when you get to line 13, so loop terminates.


RE: Removing items in a list - cap510 - Nov-01-2020

(Nov-01-2020, 09:38 PM)GOTO10 Wrote: On line 13 you are checking the value of first.lower(). Look at your logic again and make sure that is the variable you want to check at that point. Also, consider what happens on line 11 if a user entered "q" on line 10.

Thank you GOTO10-
fixed!