| | |
How to check element of list are present in other list?
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Feb 2007
Posts: 11
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Sep 2005
Posts: 133
Reputation:
Solved Threads: 58
Python Syntax (Toggle Plain Text)
a = [4,5,6] s = set(a)

For further info have a look at the documentation.
Last edited by mawe; Nov 26th, 2007 at 9:31 am.
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)
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"
May 'the Google' be with you!
![]() |
Similar Threads
- Removing elements from a list in a loop (Python)
- Quicksorting linked list - simple algorithm (C)
- how to assign a constant element of a list (C)
- Sorted linked list help (C++)
- add function of a liked list class (C)
- Check/Uncheck All (Contact List) (DaniWeb Community Feedback)
- help with this code of link list (C++)
- Merge Two Linked List...Check (C)
- Checking for duplicates in a orderedered linked list (C++)
- Feeding a value in a dropdown list to another dropdown list (PHP)
Other Threads in the Python Forum
- Previous Thread: printing some part of string
- Next Thread: HELP! how to I loop this nonstop?
| Thread Tools | Search this Thread |
accessdenied advanced application argv beginner change color command convert csv cursor def dictionary digital dynamic dynamically edit enter event examples file float format frange function google gui homework i/o import input jaunty java keyboard lapse line linux list lists loop microphone mouse movingimageswithpygame newb number numbers numeric obexftp output parameters parsing path port prime programming projects py2exe pygame pygtk pyopengl python random recursion remote return reverse scrolledtext session simple skinning smtp sprite statictext stderr string strings subprocess syntax table tennis terminal text thread threading time tkinter tlapse trick tuple tutorial ubuntu unicode unit urllib urllib2 variable voip web-scrape windows wxpython






