*Memo:
- My post explains a list and the list with indexing.
- My post explains list functions (1).
- My post explains list functions (2).
A list can be read or changed by slicing as shown below:
*Memo:
- Slicing can be done with one or more
[start:end:step]
:-
start
(Optional-Default:The index of the 1st element
):- It's a start index(inclusive).
-
end
(Optional-Default:The index of the last element + 1
):- It's an end index(exclusive).
-
step
(Optional-Default:1
):- It's the interval of indices.
- It cannot be zero.
- The
[]
with at least one:
is slicing.
-
- An iterable must be assigned to a sliced variable.
- A del statement can remove zero or more elements from a list by slicing and can remove one or more variables themselves.
1D list:
v1 = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
print(v1[:])
print(v1[::])
print(v1[0:8:1])
# ['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])
# []
v1 = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
v2 = v1[:]
v2[2:6] = [0, 1, 2, 3, 4, 5]
# v2[-6:-2] = [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]]
# v2[-6:-2] = [[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]
# v2[-6:-2: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]]
# v2[-6:-2: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:2] = []
# v2[-6:-6] = []
print(v2)
# ['A', 'B', 'C', 'D', 'E', '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']
v2 = v1[:]
del v2[2:6]
# del v2[-6:-2]
print(v2)
# ['A', 'B', 'G', 'H']
v2 = v1[:]
del v2[2:6:2]
# del v2[-6:-2:2]
print(v2)
# ['A', 'B', 'D', 'F', 'G', 'H']
v2 = v1[:]
del v2[2:2]
# del v2[-6:-6]
print(v1)
# ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
del v1, v2
print(v1)
# NameError: name 'v1' is not defined
print(v2)
# NameError: name 'v2' is not defined
2D list:
v1 = [['A', 'B', 'C', 'D'], ['E', 'F', 'G', 'H']]
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']
v1 = [['A', 'B', 'C', 'D'], ['E', 'F', 'G', 'H']]
v2 = v1[:]
v2[0][1:3] = [0, 1, 2, 3]
# v2[-2][-3:-1] = [0, 1, 2, 3]
v2[1][::2] = [0, 1]
# 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[-2][-3:-1] = [[0, 1, 2, 3]]
v2[1][::2] = [[0, 1, 2, 3], [0, 1, 2, 3]]
# v2[-1][::2] = [[0, 1, 2, 3], [0, 1, 2, 3]]
print(v2)
# [['A', [0, 1, 2, 3], 2, 3, 'D'], [[0, 1, 2, 3], 'F', [0, 1, 2, 3], 'H']]
v2 = v1[:]
del v2[0][1:3], v2[1][::2]
# del v2[-2][-3:-1], v2[-1][::2]
print(v2)
# [['A', 3, 'D'], ['F', 'H']]
3D list:
v1 = [[['A', 'B'], ['C', 'D']], [['E', 'F'], ['G', 'H']]]
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']
v1 = [[['A', 'B'], ['C', 'D']], [['E', 'F'], ['G', 'H']]]
v2 = v1[:]
v2[0][1:] = [0, 1, 2, 3]
# v2[-2][-1:] = [0, 1, 2, 3]
v2[1][0][0:] = [0, 1, 2, 3]
# v2[-1][-2][-2:] = [0, 1, 2, 3]
v2[1][1][::2] = [[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']]]
v2 = v1[:]
del v2[0][1:], v2[1][0][0:], v2[1][1][::2]
# del v2[-2][-1:], v2[-1][-2][-2:], v2[-1][-1][::2]
print(v2)
# [[['A', 'B']], [[], ['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[3:5] = ['Y', 'Z'] # v3 = v2
del v3[1:3]
print(v1) # ['X', 'Y', 'Z']
print(v2) # ['X', 'Y', 'Z']
print(v3) # ['X', 'Y', 'Z']
The variables v1
and v2
refer to the same list unless copied as shown below:
*Memo:
-
is
keyword oris
andnot
keyword can check ifv1
andv2
refer or don't refer to the same list respectively. -
list.copy(), copy.copy(),
list()
and slicing do shallow copy:-
list.copy()
has no arguments.
-
- copy.deepcopy() does deep copy.
-
copy.deepcopy()
should be used because it's safe, doing copy deeply whilelist.copy()
,copy.copy()
,list()
and slicing aren't safe, doing copy shallowly.
import copy
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, v1 is not v2)
# True False
# v2 refers to the different list from v1.
v2 = v1.copy()
v2 = copy.copy(v1)
v2 = copy.deepcopy(v1)
v2 = list(v1)
v2 = 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, v1 is not v2)
# False True
Top comments (0)