*Memos:
My post explains how to create a list and access and change a list by 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). *stepmustn't be zero.- The
[]with at least one:is slicing. - Slicing does shallow copy.
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', 'X', 'd', 'Y', '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']
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']]]
The reference of a list is stored in a variable as shown below:
*Memos:
- The variables
v1andv2and have the same references of the same list, that's why changing the list content ofv2also changes the other list content ofv1.
iskeyword can check ifv1andv2have the same references.- Again, slicing does shallow copy.
copy() can also do shallow copy. *There are no arguments.
v1 = ['a', 'b', 'c', 'd']
v2 = v1 # v2 has the same reference of the same list as v1.
v2[2] = 'X' # Changes the same list content by the same reference as v1.
# ↓↓↓
print(v1) # ['a', 'b', 'X', 'd']
print(v2) # ['a', 'b', 'X', 'd']
# ↑↑↑
print(v1 is v2) # True
v2 = v1[:] # v2 has the different reference of the different list
v2 = v1.copy() # from v1.
v2[2] = 'Y' # Changes a different list content by the different reference
# from v1.
# ↓↓↓
print(v1) # ['a', 'b', 'X', 'd']
print(v2) # ['a', 'b', 'Y', 'd']
# ↑↑↑
print(v1 is v2) # False
SOCIAL SHARE CARD GENERATOR