Remove elements of a list if is in another list

A. Esquivias :

I have two lists with some number combinations and I would like to remove the element of a list is in the other list.

I mean, lists could be:

X = [1, 2, 3, 128, 129, 134, 135, 136, 145, 146, 156, 164, 234, 235, 236, 245, 246, 266, 345]
Y = [12, 16, 26, 126]

So I would like to delete from the first list all the elements that include any element of the second list, so we would remove all items that contain '12', '16', '26' or '126'.

Ending up with a list compounded by

[1, 2, 3, 134, 135, 136, 145, 146, 156, 234, 235, 236, 245, 246, 256, 345]

Greetings and thanks in advance

kederrac :

you can use a list comprehension:

str_y = list(map(str, Y))

X = [e for e in X if all(y not in str(e) for y in str_y)]
X

output:

[1, 2, 3, 134, 135, 136, 145, 146, 156, 234, 235, 236, 245, 246, 345]

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=361206&siteId=1