twfx
twfx

Reputation: 1694

Check if list array contains element in another list

Given a list array a, and another list b and d. How can I check if list element in a exists in b (or another example, in d)? I know I can do it with a for loop of a element and check if each element is in b/d, but is there any API do it quickly?

 a = [[1,4], [17,33,2],[2,33]]

 b = [1,4,5,6]
 c = [[1,4]]

 d = [2,33]
 e = [[17,33,2],[2,33]]

Let put it this way, give list a and b, how can I write the loop below efficiently? Let say in one line using list comprehension.

        newls = []
        for sublist in a:
           newls.append(list(set(sublist).intersection(set(b))))  

Upvotes: 6

Views: 12822

Answers (4)

Abhijeet
Abhijeet

Reputation: 8771

Check if list(dictionary) contains element in another list

a = [{'id':1,'val':4},{'id':17,'val':33},{'id':2,'val':33}]
b = [1,4,5,6,17]
print([x for x in a if x['id'] in b])

Demo

Check and retrieve specific value if list(dictionary) contains element in another list

a = [{'id':1,'val':4,'code':'008'},{'id':17,'val':33,'code':'005'},{'id':2,'val':33}]
b = [1,4,5,6,17]
print([x['val'] for x in a if x['id'] in b])

Demo

Upvotes: 0

mhawke
mhawke

Reputation: 87134

I doubt that this is what you really wanted, but it is what you've asked for, i.e. a one line list comprehension that produces the same result as your for loop:

newls = [list(set(sublist).intersection(set(b))) for sublist in a]

a = [[1,4], [17,33,2],[2,33]]
b = [1,4,5,6]
>>> c = [list(set(sublist).intersection(set(b))) for sublist in a]
>>> c
[[1, 4], [], []]

You probably don't want the empty lists in there, so:

>>> c = filter(None, [list(set(sublist).intersection(set(b))) for sublist in a])
>>> c
[[1, 4]]

Note that this does not give the expected result for the second case:

a = [[1,4], [17,33,2],[2,33]]
d = [2,33]
e = filter(None, [list(set(sublist).intersection(set(d))) for sublist in a])

>>> e
[[33, 2], [33, 2]]

Upvotes: 4

Nir Alfasi
Nir Alfasi

Reputation: 53565

Like barmer mentioned in a comment above: finding the intersection of two lists can be done easily using list comprehension:

a = [[1,4], [17,33,2],[2,33]]
b = [[1,4], [1,2], [2,3], [2,33]]

print [x for x in a if x in b] # prints [[1, 4], [2, 33]]

Upvotes: 5

Lionel
Lionel

Reputation: 34844

If what you care about is compactness of code and not speed, you can use the set API, and this will tell you if there is an element that is in a, b, and c at the same time (and tell you which ones)

len(set(sum(a,[])) & set(b) & set(c).is_empty())>0

This is not too bad regarding speed because the intersection of sets is efficient, but the sum function isn't, so you may optimize the sum(a,[]) bit in any of the ways suggested in Making a flat list out of list of lists in Python

Also if you only care to know whether there is an element that's common, and not to know all the elements that exist in common, you can optimize further, and you'll have to implement dirtier and dirtier code.

Upvotes: 1

Related Questions