0

I want to remove a string in a nestled list

mylist = [
    ["School", "England", "1934"],
    ["House", "Germany", "1845"],
    ["Paris", "France", "1910"]
]

If the user types "School", "House", or "Paris" I want so that the whole line removes. I have following code:

user = input("What do you want to remove?")
    counter = 0
    for i in mylist:
        for j in i:
            if mylist[counter][0] == user:
                mylist.remove(mylist[counter])
                print(user + " removed!")
        counter = counter + 1

Now the problem is that I can't remove the third line in mylist, just the first and the second one. I think the problem is my counter byt I am not sure.

I appreciate all the help!

Gringo
  • 33
  • 8

1 Answers1

0

You can do a list-comprehension:

mylist = [
    ["School", "England", "1934"],
    ["House", "Germany", "1845"],
    ["Paris", "France", "1910"]
]

user = input("What do you want to remove? ")

mylist = [x for x in mylist if x[0] != user]

Sample run:

What do you want to remove? House
[['School', 'England', '1934'], ['Paris', 'France', '1910']][Program finished]
Austin
  • 25,759
  • 4
  • 25
  • 48