6

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))))  
2
  • 1
    Use a list comprehension to loop through a, and test whether the intersection of the sublists with the given list is non-empty. Commented Aug 21, 2014 at 3:51
  • Do you need to produce c and e, or do you just need to test? Commented Aug 21, 2014 at 4:01

4 Answers 4

5

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]]
Sign up to request clarification or add additional context in comments.

Comments

4

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]]

Comments

1

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.