-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathExercise-14-List-Remove-Duplicates.py
46 lines (35 loc) Β· 1.11 KB
/
Exercise-14-List-Remove-Duplicates.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
'''
Exercise 14: List Remove Duplicates
Write a program (function!) that takes a list and returns a new
list that contains all the elements of the first list minus all
the duplicates.
Extras:
Write two different functions to do this - one using a loop and
constructing a list, and another using sets.
Go back and do Exercise 5 using sets, and write the solution for
that in a different function.
'''
# Solution
def remove_dup_loop(input_list):
"""
Remove all duplicates in list. Using loop method.
Arguments:
input_list -- a list input
Returns:
new_list -- a list remove all the duplicates.
"""
new_list = []
new_list.extend(i for i in input_list if i not in new_list)
return new_list
def remove_dup_set(input_list):
"""
Remove all duplicates in list. Using set method.
Arguments:
input_list -- a list input
Returns:
new_list -- a list remove all the duplicates.
"""
return list(set(input_list))
a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
print(remove_dup_loop(a))
print(remove_dup_set(a))