Bezi
Bezi

Reputation: 45

intersection of one element of two nested lists?

Actually I have searched other questions about intersection of two nested lists, but I could not fix my problem, it's a little bit different. For example I have two lists

c1=[2,4,5] 
c2=[[2,23,43],[7,54,12],[4,97,52],[9,21,25],[5,34,23]]

I want to obtain the components of c2 which its first elements is same c1, it means I need to obtain:

c3=[[2,23,43],[4,97,52],[5,34,23]]

have you guys any idea about it !?

Upvotes: 0

Views: 61

Answers (1)

sacuL
sacuL

Reputation: 51335

You can use this list comprehension, which returns each element of c2 if it has an intersection with c1:

>>> [i for i in c2 if set(c1).intersection(i)]

[[2, 23, 43], [4, 97, 52], [5, 34, 23]]

Upvotes: 1

Related Questions