Created
February 19, 2018 15:29
-
-
Save iamshadmirza/1efea9df94926d2ef7857dbef167da15 to your computer and use it in GitHub Desktop.
Intersection on two lists
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
a= random.sample(range(1,30), 7) | |
b= random.sample(range(1,30), 10) | |
result1= [] | |
result2 = [] | |
result3 = [] | |
for element in a: | |
if element in b: | |
result1.append(element) | |
print(result1) | |
#this is first try | |
result2 = [element for element in a if element in b ] | |
print(result2) | |
#this will yeild duplicate solution | |
result3 = [element for element in set(a) if element in b ] | |
print(result3) | |
#this will not yeild duplicates | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment