Delete all occurrences of specific values from list of lists python

后端 未结 2 558
猫巷女王i
猫巷女王i 2021-01-27 16:40

As far as I can see this question (surprisingly?) has not been asked before - unless I am failing to spot an equivalent question due to lack of experience. (Similar questions h

2条回答
  •  感情败类
    2021-01-27 16:59

    Using list comprehension should work as:

    new_list = [[i for i in l if i not in list_A] for l in list_of_list]
    

    After that, if you want to remove empty lists, you can make:

    for i in new_list:
        if not i:
            new_list.remove(i)
    

    of, as @ferhatelmas pointed in comments:

    new_list = [i for i in new_list if i]
    

    To avoid duplicates in list_A you can convert it to a set before with list_A = set(list_A)

提交回复
热议问题