954,546 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to check element of list are present in other list?

Hi,

I have two list of any datatype supported by python. I want to check whether the any of the element of first list
is present in second list or not.

Example: I have
a = [4,5,6]
b = [1,3,8,6,7,9]

I want to check whether any element of a is present in be or not. Also without using for loop.
Is there any other way to do this or any inbuilt function of python,which will check this. Please suggest me.

Thanks

R.S.Chourasia
Newbie Poster
11 posts since Feb 2007
Reputation Points: 10
Solved Threads: 0
 

Convert them to sets and use the intersection.

woooee
Nearly a Posting Maven
2,454 posts since Dec 2006
Reputation Points: 777
Solved Threads: 714
 

Hello,

Thanks for your reply.
Since I am new to python, i do not have any idea of sets. So It would be good for me if you can give me an example of, how to it convert in to sets and how to take the intersection.

Thanks

R.S.Chourasia
Newbie Poster
11 posts since Feb 2007
Reputation Points: 10
Solved Threads: 0
 
a = [4,5,6]
s = set(a)

Now s is a set :)
For further info have a look at the documentation .

mawe
Junior Poster
133 posts since Sep 2005
Reputation Points: 19
Solved Threads: 58
 

Actually there are many ways to solve this, but here would be an example for a beginning student. Study it and try to understand it ...

a = [4,5,6]
b = [1,3,8,6,7,9]

c = []
for bx in b:
    if bx in a:
        c.append(bx)
        
if c:
    print "these are the elements of list a that are present in list b:"
    print c
else:
    print "no elements of list a are in list b"
vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

Nice and easy, vegaseat, but
Also without using for loop.
:)

mawe
Junior Poster
133 posts since Sep 2005
Reputation Points: 19
Solved Threads: 58
 

Here is the answer to do the same ...using sets..

from sets import Set
a = Set([1,2,3,4,5])
b = Set([6,7,8,9,10])
c = Set([2,4,6,8,10])
s1 = list(a.intersection(b))
s2 = list(a.intersection(c))
s3 = list(b.intersection(c))
print s1
print s2
print s3

R.S.Chourasia
Newbie Poster
11 posts since Feb 2007
Reputation Points: 10
Solved Threads: 0
 

This following if condition to check for the presence of an element in a list is just wonderful.

if bx in b:

where bx is a element that can be checked for its presence in the list b

Thank you,

systemsbiology
Newbie Poster
2 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

How do you do this when the elements of the list are dictionaries? Using sets won't work in that case. Python complains because dictionaries are not "hashable".
For example, I want a function that will tell me which elements of A are in B (the intersection of the two lists), where:
A=[{},{1:2},{3,4},{10:34}]
B=[{5,6},{1,2},{}]

jgalaz
Newbie Poster
3 posts since Mar 2012
Reputation Points: 10
Solved Threads: 0
 

I think the builtin filter method in python will also do this, but it might not be so straightforward. Vegas's answer will extend to dictionaries if you use:

for item in A.iteritems():
    for item in B.iteritems():

etc...

However if you can't use loops (which is dumb IMO) then you gotta do something else.

shoemoodoshaloo
Junior Poster
168 posts since May 2009
Reputation Points: 16
Solved Threads: 6
 
How do you do this when the elements of the list are dictionaries? Using sets won't work in that case. Python complains because dictionaries are not "hashable". For example, I want a function that will tell me which elements of A are in B (the intersection of the two lists), where: A=[{},{1:2},{3,4},{10:34}] B=[{5,6},{1,2},{}]


B is not list of dictionary, it is list of set, so it does not make sense to join them.

pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

Yes, loops are the trivial long-way solution. I was asking for a method existing in Python that would allow avoiding them.

jgalaz
Newbie Poster
3 posts since Mar 2012
Reputation Points: 10
Solved Threads: 0
 

PyTony, you are right. There are "typos" in the definition of A and B. They should be as stated in writing, lists of dictionaries:

A=[{},{1:2},{3:4},{10:34}]
B=[{5:6},{1:2},{}]

I do not wish to join them, but rather to find python methods that will return the intersection of the lists (all the elements that are present in both), the difference between them (elements only present in A, or only present in B), etc... basically, all the operations that work for sets.
The problem stems from not being able to turn A and B into a set.

jgalaz
Newbie Poster
3 posts since Mar 2012
Reputation Points: 10
Solved Threads: 0
 

Are the values for keys same, if so you can just ignore values and do set operations by sets produced from the keys of dictionaries?

pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

Post: Markdown Syntax: Formatting Help
You