-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path19_add_remove_update_lists.py
More file actions
138 lines (126 loc) · 4.34 KB
/
Copy path19_add_remove_update_lists.py
File metadata and controls
138 lines (126 loc) · 4.34 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#======================================================================================
# Add Remove & Update Lists
#--------------------------------------------------------------------------------------
# In python, lists are mutable, so you can easily
# Add, Remove, and Update Items
#======================================================================================
#--------------------------------------------------------------------------------------
# Add items to list
#--------------------------------------------------------------------------------------
# .append(value) --Add one item at the end of list
# .insert(index, value) --Add item at specific position
#--------------------------------------------------------------------------------------
letters = ['a', 'b', 'c']
# Add 'x' to the end of the list
letters.append('x')
print(letters)
# Add 'y' at the start of the list
letters.insert(0, 'y')
print(letters)
# Add 'z' between 'b' and 'c'
letters.insert(3, 'z')
print(letters)
#--------------------------------------------------------------------------------------
# Add items to nested list
#--------------------------------------------------------------------------------------
matrix = [
['a', 'b', 'c'], # Row 0
['d', 'e', 'f'], # Row 1
['g', 'h', 'i'] # Row 2
]
# Add new row to the end of matrix
matrix.append(['x', 'y', 'z'])
print(matrix)
# Add 'x' at the end of the second roe
matrix[1].append('x')
print(matrix)
# Add new roe at the start of matrix
matrix.insert(0, ['p', 'q', 'r'])
print(matrix)
# Add 'z' at the start of the first row
matrix[0].insert(0, 'z')
print(matrix)
#--------------------------------------------------------------------------------------
# Remove items from list
#--------------------------------------------------------------------------------------
# .clear() --Remove everything from list
# .remove(value) --Remove by value(Removes only the first match) & (error occurs if item doesn't exist)
# .pop(index) --Remove by index
# .pop() --pop() without index removes last item
#--------------------------------------------------------------------------------------
letters = ['a', 'b', 'c', 'd', 'e']
print(letters)
# Remove 'a' from the list
letters.remove('a') # removes only the first match
print(letters)
# Remove the first item
letters.pop(0)
print(letters)
# Remove the last item
letters.pop() # pop() without index no removes last item
print(letters)
# Remove everything from list
letters.clear() # Removes everything from list
print(letters)
#--------------------------------------------------------------------------------------
# Remove items from nested list
#--------------------------------------------------------------------------------------
matrix = [
['a', 'b', 'c'], # Row 0
['d', 'e', 'f'], # Row 1
['g', 'h', 'i'] # Row 2
]
print(matrix)
matrix.remove(['a', 'b', 'c'])
print(matrix)
# Remove last row
matrix.pop()
print(matrix,'\n')
matrix = [
['a', 'b', 'c'], # Row 0
['d', 'e', 'f'], # Row 1
['g', 'h', 'i'] # Row 2
]
# Remove 'e' from matrix
matrix[1].remove('e')
print(matrix)
# Remove 'g' from matrix
matrix[-1].pop(0)
print(matrix)
# Remove last item of the first list
matrix[0].pop()
print(matrix)
print()
#--------------------------------------------------------------------------------------
# Update items in list
#--------------------------------------------------------------------------------------
# lst[index] = value
#--------------------------------------------------------------------------------------
letters = ['a', 'b', 'c']
# Update the first item to 'x'
letters[0] = 'x'
print(letters)
# UPdate the second item to 'z
letters[1] = 'z'
print(letters)
#--------------------------------------------------------------------------------------
# Update items in nested list
#--------------------------------------------------------------------------------------
matrix = [
['a', 'b', 'c'], # Row 0
['d', 'e', 'f'], # Row 1
['g', 'h', 'i'] # Row 2
]
print(matrix)
# Update the content of the last list
matrix[-1] = ['x', 'y', 'z']
print(matrix)
# Update the first item of the first row
matrix[0][0] = '-'
print(matrix)
# Update the second item of the second row
matrix[1][1] = '-'
print(matrix)
# Update the third item of the third row
matrix[-1][-1] = '-'
print(matrix)