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

Recommended Answers

All 13 Replies

Convert them to sets and use the intersection.

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

a = [4,5,6]
s = set(a)

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

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"

Nice and easy, vegaseat, but

Also without using for loop.

:)

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

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,

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},{}]

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.

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.

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

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.

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?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.