DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Edited on

List in Python (4)

Buy Me a Coffee

*Memo:

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]
Enter fullscreen mode Exit fullscreen mode

<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]]
Enter fullscreen mode Exit fullscreen mode

<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]]]
Enter fullscreen mode Exit fullscreen mode

Be careful, a big list gets MemoryError as shown below:

v = [0, 1, 2, 3, 4] * 1000000000
# MemoryError
Enter fullscreen mode Exit fullscreen mode
v = range(1000000000)

print(list(v))
# MemoryError
Enter fullscreen mode Exit fullscreen mode
v = [x for x in range(1000000000)]
# MemoryError
Enter fullscreen mode Exit fullscreen mode

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=.
    • Error occurs if index is out of range.
  • index can be signed indices(zero and positive and negative indices).
  • A del statement 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
Enter fullscreen mode Exit fullscreen mode
v = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']

print(v[-9], v[8])
# IndexError: list index out of range
Enter fullscreen mode Exit fullscreen mode
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
Enter fullscreen mode Exit fullscreen mode

<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']]
Enter fullscreen mode Exit fullscreen mode

<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']]]
Enter fullscreen mode Exit fullscreen mode

Top comments (0)