-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodify_list.py
73 lines (57 loc) · 1.3 KB
/
modify_list.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
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
"""
lst = [1, 2, 3, 4, 5, 6]
print(modify_list(lst)) # None
print(lst) # [1, 2, 3]
modify_list(lst)
print(lst) # [1]
lst = [10, 5, 8, 3]
modify_list(lst)
print(lst) # [5, 4] """
t = [3, 5]
t[:] = [1, 5]
# m[0] = 12
print(t)
# print(m)
exit()
def modify_list(l):
i = 0
while i < len(l):
x = l[i]
if x % 2 == 0:
l[i] = x // 2
i += 1
else:
del l[i]
# below doesn't work since delete shifts index in the list
# for i, x in enumerate(l):
# if x % 2 == 0:
# l[i] = x // 2
# else:
# del l[i]
# [l[i] = x //2 if x % 2 == 0 else del l[i] for i,x in enumerate(l)]
l = [8, 9, 12]
modify_list(l)
print(l)
modify_list(l)
print(l)
modify_list(l)
print(l)
# another solution
# useful to know the following:
# >>> a = [0]
# >>> b = a
# >>> a[:] = [1]
# >>> print(b)
# [1] <--- note, change reflected by a and b
# >>> a = [2]
# >>> print(b)
# [1] <--- but now a points at something else, so no change to b
def modify_list_2(l):
l[:] = [(i // 2) for i in l if i % 2 == 0]
l = [8, 9, 12]
modify_list_2(l)
print(l)
modify_list_2(l)
print(l)
modify_list_2(l)
print(l)