Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.64 KB | None | 0 0
  1. # Write a program (function!) that takes a list and returns a new list that
  2. # contains all the elements of the first list minus all the duplicates.
  3. # Write two different functions to do this - one using a loop and constructing
  4. # a list, and another using sets.
  5. # USING SETS
  6.  
  7.  
  8. def minus_duplicates(list):
  9. numbers = set(list)
  10. list2 = list(numbers)
  11. print(list2)
  12.  
  13.  
  14. a = [1, 1, 2, 3, 5, 7, 7, 8, 16, 13, 5]
  15. minus_duplicates(a)
  16.  
  17.  
  18. # USING A LOOP AND CONSTRUCTING A LIST
  19.  
  20.  
  21. def loop(list):
  22. nothing = []
  23. for i in list:
  24. if i not in nothing:
  25. nothing.append(i)
  26. print(nothing)
  27.  
  28.  
  29. a = [1, 1, 2, 3, 5, 7, 7, 8, 16, 13, 5]
  30. loop(a)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement