Skip to content

Instantly share code, notes, and snippets.

@pinkpretty
Created September 20, 2016 12:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pinkpretty/b6f1762efcb37a4f69e6f576505e7e39 to your computer and use it in GitHub Desktop.
Save pinkpretty/b6f1762efcb37a4f69e6f576505e7e39 to your computer and use it in GitHub Desktop.
'''
this is the 5th exercise from practicepython
'''
'''
Exercise (and Solution)
Take two lists, say for example these two:
a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
and write a program that returns a list that contains only the elements that are common between the lists (without duplicates). Make sure your program works on two lists of different sizes.
Extras:
1.Randomly generate two lists to test this
2.Write this in one line of Python
'''
import random
my_random1 = [random.randrange(1,101) for _ in range (10)]
my_random2 = [random.randrange(1,101) for _ in range (10)]
common_elements = []
print(my_random1)
print(my_random2)
common_elements = [item for item in set(my_random1+my_random2) if item in my_random1,my_random2]
print(common_elements)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment