Python Forum
Removing items from list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Removing items from list
#1
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.
Reply
#2
You had this exact same problem in your last post. The answer is still the same. 'websitetodelete' is a string. The items in passwords are lists. You can't delete an item that isn't in the list, so you can't delete a string from a list of lists. You need to delete the [website, password] list, or you need to reconstruct the list, possibly like so:

passwords = [pair for pair in passwords if pair[0] != websitetodelete]
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
This works but we've not learned how to reconstruct a list.

How would I delete the [website, password] list? Would I need to take the given index of the website and delete whatever list that website is in? I'm probably going to run into the same issues where the input is a string and the list is not, as you so kindly pointed out to me. :)
Reply
#4
you can try like,
for values in passwords:
    if websitetodelete in values:
        passwords.remove(values)
This would remove [website,password] list from the passwords list
Reply
#5
That's not a good solution, you don't want to modify a list while you are looping over it.

If you haven't done list comprehensions, you can reconstruct a list with a for loop:

new_pass = []
for site_pass in passwords:
    if site_pass[0] != websitetodelete:
        new_pass.append(site_pass)
passwords = new_pass
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
Since this is basically a collection of key-value pairs (website to password), you could also use a dict, and then use dict.pop() to remove them.

>>> sites = {"yahoo": "yahoo_password", "google": "spam-is-Ta$ty"}
>>> sites
{'yahoo': 'yahoo_password', 'google': 'spam-is-Ta$ty'}
>>> "yahoo" in sites
True
>>> sites.pop("yahoo")
'yahoo_password'
>>> sites
{'google': 'spam-is-Ta$ty'}
Reply
#7
He said in another post that he has to use a list for this.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#8
But pop() also works on lists.
>>> passwords = [["yahoo","XqffoZeo"],["google","CoIushujSetu"]]
>>> passwords.pop(1)
['google', 'CoIushujSetu']
>>> passwords
[['yahoo', 'XqffoZeo']]
So find the index of the site to be deleted and use passwords.pop(index).
Reply
#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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Removing all strings in a list that are of x length Bruizeh 5 3,158 Aug-27-2021, 03:11 AM
Last Post: naughtyCat
  Removing existing tuples from a list of tuple Bruizeh 4 2,769 May-15-2021, 07:14 PM
Last Post: deanhystad
  Collisions for items in a list Idents 3 2,283 Apr-06-2021, 03:48 PM
Last Post: deanhystad
  Removing items in a list cap510 3 2,331 Nov-01-2020, 09:53 PM
Last Post: cap510
  Help with Recursive solution,list items gianniskampanakis 8 3,571 Feb-28-2020, 03:36 PM
Last Post: gianniskampanakis
  Find 'greater than' items in list johneven 2 4,457 Apr-05-2019, 07:22 AM
Last Post: perfringo
  How to add items within a list Mrocks22 2 2,663 Nov-01-2018, 08:46 PM
Last Post: Mrocks22
  How to keep duplicates and remove all other items in list? student8 1 4,937 Oct-28-2017, 05:52 AM
Last Post: heiner55
  need help removing an item from a list jhenry 4 4,187 Oct-13-2017, 08:15 AM
Last Post: buran
  Help printing any items that contains a keyword from a list Liquid_Ocelot 13 71,969 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