Skip to content

Instantly share code, notes, and snippets.

@srahuliitb
Created September 6, 2017 11:40
Show Gist options
  • Select an option

  • Save srahuliitb/4f997290c9a13bf07d42e2664579a91a to your computer and use it in GitHub Desktop.

Select an option

Save srahuliitb/4f997290c9a13bf07d42e2664579a91a to your computer and use it in GitHub Desktop.
A program that returns a list that contains only the elements that are common between two lists without duplicates.
# Prgram which returns list of common elements in two lists.
list_a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
list_b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
# Removing duplicates.
def remove_duplicates(any_list):
new_list = []
for item in any_list:
if item not in new_list:
new_list.append(item)
return new_list
new_list_a = remove_duplicates(list_a)
new_list_b = remove_duplicates(list_b)
print new_list_a
print new_list_b
# Make a new list which contains elements which are common in two lists.
def common_list(list_1, list_2):
new_common_list = []
for element in list_1:
if element in list_2:
new_common_list.append(element)
return new_common_list
print common_list(new_list_a, new_list_b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment