*Memo:
- My post explains a list (1).
- My post explains a list (2).
- My post explains a list (3).
- My post explains a list (5).
- My post explains a list (6).
A list comprehension can create a list as shown below:
<1D list>:
sample = [0, 1, 2, 3, 4, 5, 6, 7]
v = [x**2 for x in sample]
print(v)
# [0, 1, 4, 9, 16, 25, 36, 49]
<2D list>:
sample = [[0, 1, 2, 3], [4, 5, 6, 7]]
v = [[y**2 for y in x] for x in sample]
print(v)
# [[0, 1, 4, 9], [16, 25, 36, 49]]
<3D list>:
sample = [[[0, 1], [2, 3]], [[4, 5], [6, 7]]]
v = [[[z**2 for z in y] for y in x] for x in sample]
print(v)
# [[[0, 1], [4, 9]], [[16, 25], [36, 49]]]
Be careful, a big list gets MemoryError as shown below:
v = [0, 1, 2, 3, 4] * 1000000000
# MemoryError
v = range(1000000000)
print(list(v))
# MemoryError
v = [x for x in range(1000000000)]
# MemoryError
A list can be read by indexing or changed by indexing and a del statement as shown below:
*Memo:
- Indexing can be done with one or more
[index]in the range[The 1st index, The last index]:-
index(Required-Type:int):- Don't use
index=.
- Don't use
- Error occurs if
indexis out of range.
-
-
indexcan be signed indices(zero and positive and negative indices). - A
delstatement can remove one or more elements from a list by indexing and can remove one or more variables themselves.
<1D list>:
v = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
print(v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7])
print(v[-8], v[-7], v[-6], v[-5], v[-4], v[-3], v[-2], v[-1])
# a b c d e f g h
v[1] = 'B'
v[-7] = 'B'
print(v)
# ['a', 'B', 'c', 'd', 'e', 'f', 'g', 'h']
v[3] = 'D'
v[-5] = 'D'
print(v)
# ['a', 'B', 'c', 'D', 'e', 'f', 'g', 'h']
v[6] = 'G'
v[-2] = 'G'
v[1], v[3], v[6] = 'B', 'D', 'G'
v[-7], v[-5], v[-2] = 'B', 'D', 'G'
print(v)
# ['a', 'B', 'c', 'D', 'e', 'f', 'G', 'h']
del v[1], v[2], v[4]
# del v[-7], v[-5], v[-2]
print(v)
# ['a', 'c', 'e', 'f', 'h']
del v
print(v)
# NameError: name 'v' is not defined
v = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
print(v[-9], v[8])
# IndexError: list index out of range
v = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
v[-9], v[8] = 'X', 'Y'
del v[-9], v[8]
# IndexError: list assignment index out of range
<2D list>:
v = [['a', 'b', 'c', 'd'], ['e', 'f', 'g', 'h']]
print(v[0], v[1])
print(v[-2], v[-1])
# ['a', 'b', 'c', 'd'] ['e', 'f', 'g', 'h']
print(v[0][0], v[0][1], v[0][2], v[0][3], v[1][0], v[1][1], v[1][2], v[1][3])
print(v[-2][-4], v[-2][-3], v[-2][-2], v[-2][-1],
v[-1][-4], v[-1][-3], v[-1][-2], v[-1][-1])
# a b c d e f g h
v[0][1] = 'B'
v[-2][-3] = 'B'
print(v)
# [['a', 'B', 'c', 'd'], ['e', 'f', 'g', 'h']]
v[1] = ['E', 'F', 'G', 'H']
v[-1] = ['E', 'F', 'G', 'H']
v[0][1], v[1] = 'B', ['E', 'F', 'G', 'H']
v[-2][-3], v[-1] = 'B', ['E', 'F', 'G', 'H']
print(v)
# [['a', 'B', 'c', 'd'], ['E', 'F', 'G', 'H']]
del v[0][1], v[1]
# del v[-2][-3], v[-1]
print(v)
# [['a', 'c', 'd']]
<3D list>:
v = [[['a', 'b'], ['c', 'd']], [['e', 'f'], ['g', 'h']]]
print(v[0], v[1])
print(v[-2], v[-1])
# [['a', 'b'], ['c', 'd']] [['e', 'f'], ['g', 'h']]
print(v[0][0], v[0][1], v[1][0], v[1][1])
print(v[-2][-2], v[-2][-1], v[-1][-2], v[-1][-1])
# ['a', 'b'] ['c', 'd'] ['e', 'f'] ['g', 'h']
print(v[0][0][0], v[0][0][1], v[0][1][0], v[0][1][1], v[1][0][0],
v[1][0][1], v[1][1][0], v[1][1][1])
print(v[-2][-2][-2], v[-2][-2][-1], v[-2][-1][-2], v[-2][-1][-1],
v[-1][-2][-2], v[-1][-2][-1], v[-1][-1][-2], v[-1][-1][-1])
# a b c d e f g h
v[0][0][1] = 'B'
v[-2][-2][-1] = 'B'
print(v)
# [[['a', 'B'], ['c', 'd']], [['e', 'f'], ['g', 'h']]]
v[0][1] = 'C'
v[-2][-1] = 'C'
print(v)
# [[['a', 'B'], 'C'], [['e', 'f'], ['g', 'h']]]
v[1] = ['D', ['E', 'F']]
v[-1] = ['D', ['E', 'F']]
v[0][0][1], v[0][1], v[1] = 'B', 'C', ['D', ['E', 'F']]
v[-2][-2][-1], v[-2][-1], v[-1] = 'B', 'C', ['D', ['E', 'F']]
print(v)
# [[['a', 'B'], 'C'], ['D', ['E', 'F']]]
del v[0][0][1], v[0][1], v[1]
# del v[-2][-2][-1], v[-2][-1], v[-1]
print(v)
# [[['a']]]
Top comments (0)