How to remove the items in a list that are in a second list in python?
问题 I have a list of lists, and another list, and I want to remove all of the items from the list of lists that are in the second list. first = [[1,3,2,4],[1,2],[3,4,2,5,1]] to_remove = [1,2] How would I go about doing this problem in general? There are also similar questions on here but nothing has helped. 回答1: An elegant solution using sets : first = [[1,3,2,4],[1,2],[3,4,2,5,1]] to_remove = [1,2] result = [list(set(x).difference(to_remove)) for x in first] result [[3, 4], [], [3, 4, 5]] 回答2: