*Memos:
- My post explains a list and the list with indexing.
- My post explains the useful functions for a list (1).
- My post explains the useful functions for a list (2).
- My post explains variable assignment.
- My post explains shallow copy and deep copy.
You can access and change a list by slicing as shown below:
*Memos:
- A list is mutable.
- Slicing can be done with one or more
[start:end:step]
. -
start
(Optional-Default:The index of the 1st element
). -
end
(Optional-Default:The index of the last element - 1
). -
step
(Optional-Default:1
). *step
mustn't be zero. - The
[]
with at least one:
is slicing. - Slicing does shallow copy.
- An iterable must be assigned to a sliced variable.
v1 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'] # 1D list print(v1[:]) print(v1[::]) # ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'] print(v1[::2]) # ['a', 'c', 'e', 'g'] print(v1[::-2]) # ['h', 'f', 'd', 'b'] print(v1[2:]) print(v1[-6:]) print(v1[2::]) print(v1[-6::]) # ['c', 'd', 'e', 'f', 'g', 'h'] print(v1[2::2]) print(v1[-6::2]) # ['c', 'e', 'g'] print(v1[2::-2]) print(v1[-6::-2]) # ['c', 'a'] print(v1[:6]) print(v1[:-2]) print(v1[:6:]) print(v1[:-2:]) # ['a', 'b', 'c', 'd', 'e', 'f'] print(v1[:6:2]) print(v1[:-2:2]) # ['a', 'c', 'e'] print(v1[:6:-2]) print(v1[:-2:-2]) # ['h'] print(v1[2:6]) print(v1[-6:-2]) print(v1[2:6:]) print(v1[-6:-2:]) # ['c', 'd', 'e', 'f'] print(v1[2:6:2]) print(v1[-6:-2:2]) # ['c', 'e'] print(v1[2:6:-2]) print(v1[-6:-2:-2]) # [] v2 = v1[:] v2[2:6] = [0, 1, 2, 3, 4, 5] print(v2) # ['a', 'b', 0, 1, 2, 3, 4, 5, 'g', 'h'] v2 = v1[:] v2[2:6] = [[0, 1, 2, 3, 4, 5]] print(v2) # ['a', 'b', [0, 1, 2, 3, 4, 5], 'g', 'h'] v2 = v1[:] v2[2:6:2] = [0, 1] print(v2) # ['a', 'b', 0, 'd', 1, 'f', 'g', 'h'] v2 = v1[:] v2[2:6:2] = [[0, 1, 2], [3, 4, 5]] print(v2) # ['a', 'b', [0, 1, 2], 'd', [3, 4, 5], 'f', 'g', 'h'] v2 = v1[:] v2[2:6] = 0 # TypeError: must assign iterable to extended slice
v1 = [['a', 'b', 'c', 'd'], ['e', 'f', 'g', 'h']] # 2D list print(v1[:]) print(v1[::]) print(v1[:][:]) print(v1[::][::]) # [['a', 'b', 'c', 'd'], ['e', 'f', 'g', 'h']] print(v1[0][:]) print(v1[0][::]) print(v1[-2][:]) print(v1[-2][::]) # ['a', 'b', 'c', 'd'] print(v1[0][::2]) print(v1[-2][::2]) # ['a', 'c'] print(v1[0][::-2]) print(v1[-2][::-2]) # ['d', 'b'] print(v1[1][:]) print(v1[1][::]) print(v1[-1][:]) print(v1[-1][::]) # ['e', 'f', 'g', 'h'] print(v1[1][::2]) print(v1[-1][::2]) # ['e', 'g'] print(v1[1][::-2]) print(v1[-1][::-2]) # ['h', 'f'] v2 = v1[:] v2[0][1:3] = [0, 1, 2, 3] v2[1][::2] = [0, 1] print(v2) # [['a', 0, 1, 2, 3, 'd'], [0, 'f', 1, 'h']] v2 = v1[:] v2[0][1:3] = [[0, 1, 2, 3]] v2[1][::2] = [[0, 1, 2, 3], [0, 1, 2, 3]] print(v2) # [['a', [0, 1, 2, 3], 'd'], [[0, 1, 2, 3], 'f', [0, 1, 2, 3], 'h']]
v1 = [[['a', 'b'], ['c', 'd']], [['e', 'f'], ['g', 'h']]] # 3D list print(v1[:]) print(v1[::]) print(v1[:][:]) print(v1[::][::]) print(v1[:][:][:]) print(v1[::][::][::]) # [[['a', 'b'], ['c', 'd']], [['e', 'f'], ['g', 'h']]] print(v1[0][:]) print(v1[0][::]) print(v1[-2][:]) print(v1[-2][::]) # [['a', 'b'], ['c', 'd']] print(v1[1][:]) print(v1[1][::]) print(v1[-1][:]) print(v1[-1][::]) # [['e', 'f'], ['g', 'h']] print(v1[0][0][:]) print(v1[0][0][::]) print(v1[-2][-2][:]) print(v1[-2][-2][::]) # ['a', 'b'] print(v1[0][0][::2]) print(v1[-2][-2][::2]) # ['a'] print(v1[0][0][::-2]) print(v1[-2][-2][::-2]) # ['b'] print(v1[0][1][:]) print(v1[0][1][::]) print(v1[-2][-1][:]) print(v1[-2][-1][::]) # ['c', 'd'] print(v1[0][1][::2]) print(v1[-2][-1][::2]) # ['c'] print(v1[0][1][::-2]) print(v1[-2][-1][::-2]) # ['d'] print(v1[1][0][:]) print(v1[1][0][::]) print(v1[-1][-2][:]) print(v1[-1][-2][::]) # ['e', 'f'] print(v1[1][0][::2]) print(v1[-1][-2][::2]) # ['e'] print(v1[1][0][::-2]) print(v1[-1][-2][::-2]) # ['f'] print(v1[1][1][:]) print(v1[1][1][::]) print(v1[-1][-1][:]) print(v1[-1][-1][::]) # ['g', 'h'] print(v1[1][1][::2]) print(v1[-1][-1][::2]) # ['g'] print(v1[1][1][::-2]) print(v1[-1][-1][::-2]) # ['h'] v2 = v1[:] v2[0][1:] = [0, 1, 2, 3] v2[1][0][0:] = [0, 1, 2, 3] v2[1][1][::2] = [[0, 1, 2, 3]] print(v2) # [[['a', 'b'], 0, 1, 2, 3], [[0, 1, 2, 3], [[0, 1, 2, 3], 'h']]]
A list can be continuously used through multiple variables as shown below:
v1 = v2 = v3 = ['a', 'b', 'c', 'd', 'e'] # Equivalent # v1 = ['a', 'b', 'c', 'd', 'e'] v1[0] = 'X' # v2 = v1 v2[2] = 'Y' # v3 = v2 v3[4] = 'Z' print(v1) # ['X', 'b', 'Y', 'd', 'Z'] print(v2) # ['X', 'b', 'Y', 'd', 'Z'] print(v3) # ['X', 'b', 'Y', 'd', 'Z']
The variables v1
and v2
refer to the same list unless copied as shown below:
*Memos:
-
is
keyword can check ifv1
andv2
refer to the same list. - Again, slicing does shallow copy.
- copy() can also do shallow copy. *There are no arguments.
- deepcopy() can do deep copy. *There are no arguments.
-
deepcopy()
should be used because it's safe, doing copy deeply whilecopy()
isn't safe, doing copy shallowly.
from copy import deepcopy v1 = ['a', 'b', 'c', 'd', 'e'] v2 = v1 # v2 refers to the same list as v1. v2[2] = 'X' # Changes the same list as v1. # ↓↓↓ print(v1) # ['a', 'b', 'X', 'd', 'e'] print(v2) # ['a', 'b', 'X', 'd', 'e'] # ↑↑↑ print(v1 is v2) # True v2 = v1[:] # v2 refers to the different list from v1. v2 = v1.copy() v2 = deepcopy(v1) v2[2] = 'Y' # Changes the different list from v1. # ↓↓↓ print(v1) # ['a', 'b', 'X', 'd', 'e'] print(v2) # ['a', 'b', 'Y', 'd', 'e'] # ↑↑↑ print(v1 is v2) # False
Top comments (0)