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

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Feb 2007
Posts: 11
Reputation: R.S.Chourasia is an unknown quantity at this point 
Solved Threads: 0
R.S.Chourasia R.S.Chourasia is offline Offline
Newbie Poster

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

 
0
  #1
Nov 23rd, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,035
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 292
woooee woooee is offline Offline
Veteran Poster

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

 
0
  #2
Nov 24th, 2007
Convert them to sets and use the intersection.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 11
Reputation: R.S.Chourasia is an unknown quantity at this point 
Solved Threads: 0
R.S.Chourasia R.S.Chourasia is offline Offline
Newbie Poster

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

 
0
  #3
Nov 26th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 133
Reputation: mawe is an unknown quantity at this point 
Solved Threads: 58
mawe mawe is offline Offline
Junior Poster

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

 
0
  #4
Nov 26th, 2007
  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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,062
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 936
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

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

 
0
  #5
Nov 26th, 2007
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 ...
  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"
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 133
Reputation: mawe is an unknown quantity at this point 
Solved Threads: 58
mawe mawe is offline Offline
Junior Poster

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

 
0
  #6
Nov 26th, 2007
Nice and easy, vegaseat, but
Originally Posted by R.S.Chourasia
Also without using for loop.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 11
Reputation: R.S.Chourasia is an unknown quantity at this point 
Solved Threads: 0
R.S.Chourasia R.S.Chourasia is offline Offline
Newbie Poster

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

 
0
  #7
Nov 24th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 2
Reputation: systemsbiology is an unknown quantity at this point 
Solved Threads: 0
systemsbiology systemsbiology is offline Offline
Newbie Poster

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

 
0
  #8
Feb 26th, 2009
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,
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC