Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python remove sublist in a list if specific element inside this sublist

Tags:

python

list

For example

list_of_specific_element = [2,13]
list = [[1, 0], [2, 1], [2, 3], [13, 12], [13, 14], [15, 13]]

I want the sublist including any value inside the list of specific element be removed from the list.
So the element [2,1],[2,3],[13,12],[13,14] should be removed from the list.
the final output list should be[[1,0],[15,13]]

like image 655
Ch Y Duan Avatar asked Dec 19 '22 07:12

Ch Y Duan


1 Answers

listy=[elem for elem in listy if (elem[0] not in list_of_specific_element) and (elem[1] not in list_of_specific_element)]

Using list comprehension one-liner

like image 142
whackamadoodle3000 Avatar answered Dec 21 '22 09:12

whackamadoodle3000