Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- a = [1,1,2,3,5,8,13,21,34,55,89]
- b = [i for i in range(1,14)] # generating a list of numbers with list comprehension
- # Defining a function that takes 2 lists as arguments and returns 1 list.
- # Functions allow us to use the same code multiple times with very little changes.
- def get_duplicates(first_list, second_list):
- # set(a+b) is a list of all values (without duplications
- # notice the use of 'if' statement and 'and' operator inside the list comprehension
- list_of_duplicates = [i for i in set(first_list+second_list) if i in first_list and i in second_list]
- # returning the list for allowing future use.
- return list_of_duplicates
- # Now we can use our function multiple times, in multiple ways.
- print('first list:',a)
- print('second list:',b)
- print('\nGetting duplicates from both list:')
- print(get_duplicates(a,b))
- # Lets use our function with some other list:
- c = [i for i in range(30,90,5)]
- print('\nnew list c:', c)
- print('Getting duplicates from new list:')
- print(get_duplicates(a,c))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement