Advertisement
Guest User

get_duplicates_python3v2

a guest
Apr 4th, 2020
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.01 KB | None | 0 0
  1. a = [1,1,2,3,5,8,13,21,34,55,89]
  2. b = [i for i in range(1,14)]    # generating a list of numbers with list comprehension
  3.  
  4. # Defining a function that takes 2 lists as arguments and returns 1 list.
  5. # Functions allow us to use the same code multiple times with very little changes.
  6. def get_duplicates(first_list, second_list):
  7.     # set(a+b) is a list of all values (without duplications
  8.     # notice the use of 'if' statement and 'and' operator inside the list comprehension
  9.     list_of_duplicates = [i for i in set(first_list+second_list) if i in first_list and i in second_list]
  10.     # returning the list for allowing future use.
  11.     return list_of_duplicates
  12.  
  13.  
  14. # Now we can use our function multiple times, in multiple ways.
  15. print('first list:',a)
  16. print('second list:',b)
  17. print('\nGetting duplicates from both list:')
  18. print(get_duplicates(a,b))
  19.  
  20.  
  21. # Lets use our function with some other list:
  22. c = [i for i in range(30,90,5)]
  23. print('\nnew list c:', c)
  24. print('Getting duplicates from new list:')
  25. print(get_duplicates(a,c))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement