Skip to content

Instantly share code, notes, and snippets.

@mariopepe
Created January 21, 2017 14:06
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 mariopepe/98e83da30198acfa11a34a80397b94ae to your computer and use it in GitHub Desktop.
Save mariopepe/98e83da30198acfa11a34a80397b94ae to your computer and use it in GitHub Desktop.
05 List Overlap
# 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.
# exercise from http://www.practicepython.org/exercise/2014/03/05/05-list-overlap.html
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]
c = []
# Create list c
for num in a:
if num in b:
c.append(num)
# convert list c to 'set' to remove duplicates
c = set(c)
# reconvert to list
c = list(c)
print(c)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment