Advertisement
karlakmkj

Practice - remove duplicates

Feb 17th, 2021
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.60 KB | None | 0 0
  1. '''
  2. Write a function remove_duplicates that takes in a list and removes elements of the list that are the same.
  3.  
  4. The order in which you present your output does not matter.
  5. Do not modify the list you take as input! Instead, return a new list.
  6. '''
  7. # Solution 1
  8. def remove_duplicates(list_dup):
  9.   count_list = set(list_dup)
  10.   return list(count_list)
  11.  
  12. print remove_duplicates([1, 1, 2, 2])
  13.  
  14. # Solution 2
  15. def remove_duplicates(list_dup):
  16.   new_list =[]
  17.   for item in list_dup:
  18.     if item not in new_list:
  19.      new_list.append(item)
  20.   return new_list
  21.  
  22. print remove_duplicates([1, 1, 2, 2])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement