-
-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathhow_to_empty_a_list.py
More file actions
88 lines (64 loc) · 2.03 KB
/
how_to_empty_a_list.py
File metadata and controls
88 lines (64 loc) · 2.03 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
"""
Tests the performance of all of the solutions listed in the following article:
https://therenegadecoder.com/code/how-to-empty-a-list-in-python/
"""
from test_bench import test_bench
def control(anime: list[str]) -> None:
"""
Provides a control scenario for testing. In this case, all of the solutions
rely on copying the input list, so the control function accounts for that.
:param anime: a list of anime
"""
anime = anime.copy()
def empty_list_by_hand(anime: list[str]) -> None:
"""
Empties a list by repeatedly removing elements from a list.
:param anime: a list of anime
"""
anime = anime.copy()
while anime:
anime.pop()
def empty_list_by_assignment(anime: list[str]) -> None:
"""
Empties a list by reassigning the reference.
:param anime: a list of anime
"""
anime = anime.copy()
anime = [] # Wouldn't actually work as a function
def empty_list_by_clear(anime: list[str]) -> None:
"""
Empties a list by calling the clear method.
:param anime: a list of anime
"""
anime = anime.copy()
anime.clear()
def empty_list_by_del(anime: list[str]) -> None:
"""
Empties a list by deleting a slice of the list.
:param anime: a list of anime
"""
anime = anime.copy()
del anime[:]
def empty_list_by_slice_assignment(anime: list[str]) -> None:
"""
Empties a list by replacing a slice of the list with an empty list.
:param anime: a list of anime
"""
anime = anime.copy()
anime[:] = []
def empty_list_by_multiplication(anime: list[str]) -> None:
"""
Empties a list by multiplication.
:param anime: a list of anime
"""
anime = anime.copy()
anime *= 0 # Also, would not work as a function
if __name__ == "__main__":
test_bench(
{
"Empty List": [[]],
"One Item List": [["Your Lie in April"]],
"Small List": [["My Hero Academia", "Attack on Titan", "Steins;Gate"]],
"Large List": [["One Punch Man"] * 100]
}
)