944,110 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 28639
  • Python RSS
Nov 23rd, 2007
0

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

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
R.S.Chourasia is offline Offline
11 posts
since Feb 2007
Nov 24th, 2007
0

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

Convert them to sets and use the intersection.
Reputation Points: 741
Solved Threads: 693
Nearly a Posting Maven
woooee is offline Offline
2,307 posts
since Dec 2006
Nov 26th, 2007
0

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

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
R.S.Chourasia is offline Offline
11 posts
since Feb 2007
Nov 26th, 2007
0

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

Python Syntax (Toggle Plain Text)
  1. a = [4,5,6]
  2. s = set(a)
Now s is a set
For further info have a look at the documentation.
Last edited by mawe; Nov 26th, 2007 at 9:31 am.
Reputation Points: 19
Solved Threads: 58
Junior Poster
mawe is offline Offline
133 posts
since Sep 2005
Nov 26th, 2007
0

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

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 ...
python Syntax (Toggle Plain Text)
  1. a = [4,5,6]
  2. b = [1,3,8,6,7,9]
  3.  
  4. c = []
  5. for bx in b:
  6. if bx in a:
  7. c.append(bx)
  8.  
  9. if c:
  10. print "these are the elements of list a that are present in list b:"
  11. print c
  12. else:
  13. print "no elements of list a are in list b"
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Nov 26th, 2007
0

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

Nice and easy, vegaseat, but
Quote originally posted by R.S.Chourasia ...
Also without using for loop.
Reputation Points: 19
Solved Threads: 58
Junior Poster
mawe is offline Offline
133 posts
since Sep 2005
Nov 24th, 2008
0

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

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
R.S.Chourasia is offline Offline
11 posts
since Feb 2007
Feb 26th, 2009
0

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

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,
Reputation Points: 10
Solved Threads: 0
Newbie Poster
systemsbiology is offline Offline
2 posts
since Feb 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: printing some part of string
Next Thread in Python Forum Timeline: HELP! how to I loop this nonstop?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC