简体   繁体   中英

Python - remove list from lists

I have a list from which I would like to delete a list so that I can have the required answer.

list = [["hello","son", 52],["welcome","home",65],["good","work",6]]
name = input("Name: ")
if name in list:
    del list[name]
    print("name + " deleted")
else:
    print("invalid name")

How can I delete list so that I get this answer:

list = [["hello", "son", 52], ["good", "work", 6]]

Then add a new list to get this result:

list = [["hello", "son", 52], ["good", "work", 6],["see","you","soon"]]

You would need to iterate through the list as the key that you are using to evaluate condition is in the sub list.

mylist = [["hello","son", 52],["welcome","home",65],["good","work",6]]
name = 'hello' # 'son' would also work
for i, sub_list in enumerate(mylist):
        if name in sub_list:
                del mylist[i]
                print(name + " deleted")
                break

print(mylist)

For a given example, where you want to specify bunch of properties for a name; I would have considered using dictionary with name as key.

My recommendation would be to loop through the list of lists using nested while loops! Check to see if the "name" you asked for is present at the index of the list, and if it is, pop or remove it using the location "[x[y]"(depending on what you want to do with the old/new list. I am a little unclear based on your phrasing).

Hope that helps!

Replace the index with what element to use in search. (I assumed index=1 )

>>> list = [["hello","son", 52],["welcome","home",65],["good","work",6]]
>>> name="home"
>>> list
[['hello', 'son', 52], ['welcome', 'home', 65], ['good', 'work', 6]]
>>> list = [rec for rec in list if rec[1] != name]
>>> list
[['hello', 'son', 52], ['good', 'work', 6]]

Recommended to use Dictionary for this types of data.

Rather than a loop, why not filter() with a lambda function? Check it out:

>>> l = [["hello","son", 52],["welcome","home",65],["good","work",6]]
>>> name = "home"
>>> filter(lambda sub_list: name not in sub_list, l)
[['hello', 'son', 52], ['good', 'work', 6]]
>>>

https://docs.python.org/2/library/functions.html#filter for more information on filter() . Hope this helps.

Edit:

Didn't see your last question about adding another list to get the second answer. Try using the append() method.

>>> l = [["first", "list"]]
>>> m = ["second", "list"]
>>> l.append(m)
>>> l
[['first', 'list'], ['second', 'list']]
>>>

Don't use list as a variable name , its a type.

You can simply use a function and use two parameters one for list and another for that word which you want to remove.

list_1 = [["hello","son", 52],["welcome","home",65],["good","work",6]]

def deleting(list_1,del_name):
    for sub_list in list_1:
        if del_name in sub_list:
            list_1.remove(sub_list)
    return list_1
print(deleting(list_1,'hello'))

output:

[['welcome', 'home', 65], ['good', 'work', 6]]

As you commented out:

if we use name as input vaiable like name = input("Enter name: ").title() is it possible to delete a list in lists and same output as above

Here is the updated solution:

list_1 = [["hello","son", 52],["welcome","home",65],["good","work",6]]

def deleting(list_1):
    del_name=input("Enter name: ")
    for sub_list in list_1:
        if del_name in sub_list:
            list_1.remove(sub_list)
    return list_1
print(deleting(list_1))

output:

Enter name: hello
[['welcome', 'home', 65], ['good', 'work', 6]]

If you want to remove this list of indices from a list of lists, you should try it by for and while loops:

Index=[1,4,6,9]

list of lists:

Coordinates=[[2,3,4,5],[6,7,8,9],[10,11,12,13],[14,15,16,17],[18,19,20,21],[22,23,24,25],[26,27,28,29],[30,31,32,33],[34,35,36,37],[38,39,40,41]]

The output would be:

Coordinates=[[2,3,4,5],[10,11,12,13],[14,15,16,17],[22,23,24,25],[30,31,32,33],[34,35,36,37]]

I use this code: at first sort list of indices:

sorted(Index)
while(len(Index)>0):
    del Coordinates[Index[0]]
    del Index[0]
    for i in range(len(Index)):
        Index[i]-=1
print(Coordinates)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM