Python Forum
Removing items from list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Removing items from list
#9
(Dec-12-2019, 03:19 AM)slackerman73 Wrote: I'm trying to remove an item from the passwords list.

I keep getting Traceback (most recent call last):
passwords.remove(websitetodelete) #This line is where I'm hung up. Trying to remove the websitetodelete from the previous line. I'm sure i'm not referencing the list correctly.
ValueError: list.remove(x): x not in list

Not sure

passwords = [["yahoo","XqffoZeo"],["google","CoIushujSetu"]]

while True:
    print("What would you like to do:")
    print(" 1. Open password file")
    print(" 2. Lookup a password")
    print(" 3. Add a password")
    print(" 4. Save password file")
    print(" 5. Print the encrypted password list (for testing)")
    print(" 6. Quit program")
    print(" 7. Delete a password")
    print("Please enter a number (1-7)")
    choice = input()

    if(choice == '7'): #Delete a password
        print("Which website do you want to delete?")
        for keyvalue in passwords:
            print(keyvalue[0]) #Printing the list of websites for the user to choose from.
        websitetodelete = input()  #Accepting the user's input for the website to remove.
        passwords.remove(websitetodelete)  #This line is where I'm hung up.  Trying to remove the websitetodelete from the previous line.  I'm sure i'm not referencing the list correctly.

If this is the way that the assignment requires that you do it and you do not understand list reconstruction then you need to find the entry that contains the website using a loop.
You've got everything else right, but you are trying to delete something that doesn't exist in the list. The lists remove function must contain the entire entry, where as you are only trying to remove part.

"yahoo" # This is what you have

["yahoo","XqffoZeo"] # This is what you need
Here is the loop that will give you the complete entry
for combo in passwords: # Read each website / password combo in the passwords list
    if combo[0] == websitetodelete: # If it matches the site the user specified
        websitetodelete = combo # Update the query to contain the entire combo
        break # Stop looking because you've found it
passwords.remove(websitetodelete) # Now this command will work
Reply


Messages In This Thread
Removing items from list - by slackerman73 - Dec-12-2019, 03:19 AM
RE: Removing items from list - by ichabod801 - Dec-12-2019, 03:25 AM
RE: Removing items from list - by slackerman73 - Dec-12-2019, 04:07 AM
RE: Removing items from list - by Malt - Dec-12-2019, 08:37 AM
RE: Removing items from list - by ichabod801 - Dec-12-2019, 02:30 PM
RE: Removing items from list - by nilamo - Dec-12-2019, 08:26 PM
RE: Removing items from list - by ichabod801 - Dec-12-2019, 08:27 PM
RE: Removing items from list - by ibreeden - Dec-13-2019, 12:36 PM
RE: Removing items from list - by Clunk_Head - Dec-13-2019, 05:39 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to parse and group hierarchical list items from an unindented string in Python? ann23fr 2 327 Today, 05:39 AM
Last Post: Pedroski55
  Removing all strings in a list that are of x length Bruizeh 5 3,287 Aug-27-2021, 03:11 AM
Last Post: naughtyCat
  Removing existing tuples from a list of tuple Bruizeh 4 2,863 May-15-2021, 07:14 PM
Last Post: deanhystad
  Collisions for items in a list Idents 3 2,353 Apr-06-2021, 03:48 PM
Last Post: deanhystad
  Removing items in a list cap510 3 2,386 Nov-01-2020, 09:53 PM
Last Post: cap510
  Help with Recursive solution,list items gianniskampanakis 8 3,692 Feb-28-2020, 03:36 PM
Last Post: gianniskampanakis
  Find 'greater than' items in list johneven 2 4,516 Apr-05-2019, 07:22 AM
Last Post: perfringo
  How to add items within a list Mrocks22 2 2,732 Nov-01-2018, 08:46 PM
Last Post: Mrocks22
  How to keep duplicates and remove all other items in list? student8 1 4,997 Oct-28-2017, 05:52 AM
Last Post: heiner55
  need help removing an item from a list jhenry 4 4,260 Oct-13-2017, 08:15 AM
Last Post: buran

Forum Jump:

User Panel Messages

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