Skip to content

Instantly share code, notes, and snippets.

@iamshadmirza
Created February 19, 2018 15:29
Show Gist options
  • Save iamshadmirza/1efea9df94926d2ef7857dbef167da15 to your computer and use it in GitHub Desktop.
Save iamshadmirza/1efea9df94926d2ef7857dbef167da15 to your computer and use it in GitHub Desktop.
Intersection on two lists
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