Python Forum
Removing items in a list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Removing items in a list
#1
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]
Larz60+ write Nov-01-2020, 09:31 PM:
Please post all code, output and errors (it it's 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.

I have fixed this for you this time. Please use bbcode tags in future posts.
Reply
#2
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.
Reply
#3
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.
Reply
#4
(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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Removing all strings in a list that are of x length Bruizeh 5 3,187 Aug-27-2021, 03:11 AM
Last Post: naughtyCat
  Removing existing tuples from a list of tuple Bruizeh 4 2,781 May-15-2021, 07:14 PM
Last Post: deanhystad
  Collisions for items in a list Idents 3 2,295 Apr-06-2021, 03:48 PM
Last Post: deanhystad
  Help with Recursive solution,list items gianniskampanakis 8 3,584 Feb-28-2020, 03:36 PM
Last Post: gianniskampanakis
  Removing items from list slackerman73 8 4,424 Dec-13-2019, 05:39 PM
Last Post: Clunk_Head
  Find 'greater than' items in list johneven 2 4,469 Apr-05-2019, 07:22 AM
Last Post: perfringo
  How to add items within a list Mrocks22 2 2,676 Nov-01-2018, 08:46 PM
Last Post: Mrocks22
  How to keep duplicates and remove all other items in list? student8 1 4,943 Oct-28-2017, 05:52 AM
Last Post: heiner55
  need help removing an item from a list jhenry 4 4,199 Oct-13-2017, 08:15 AM
Last Post: buran
  Help printing any items that contains a keyword from a list Liquid_Ocelot 13 73,613 May-06-2017, 10:41 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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