DEV Community

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

Posted on • Edited on

List in Python (3)

Buy Me a Coffee

*Memo:

A list can be iterated with a for statement as shown below:

<1D list>:

for x in [0, 1, 2, 3, 4]:
    print(x)
# 0
# 1
# 2
# 3
# 4
Enter fullscreen mode Exit fullscreen mode

<2D list>:

for x in [[0, 1, 2, 3], [4, 5, 6, 7]]:
    for y in x:
        print(y)
# 0
# 1
# 2
# 3
# 4
# 5
# 6
# 7
Enter fullscreen mode Exit fullscreen mode

<3D list>:

for x in [[[0, 1], [2, 3]], [[4, 5], [6, 7]]]:
    for y in x:
        for z in y:
            print(z)
# 0
# 1
# 2
# 3
# 4
# 5
# 6
# 7
Enter fullscreen mode Exit fullscreen mode

A list can be unpacked with an assignment and for statement, function and * but not with ** as shown below:

v1, v2, v3 = [0, 1, 2]

print(v1, v2, v3)
# 0 1 2
Enter fullscreen mode Exit fullscreen mode
v1, *v2, v3 = [0, 1, 2, 3, 4, 5]

print(v1, v2, v3)  # 0 [1, 2, 3, 4] 5
print(v1, *v2, v3) # 0 1 2 3 4 5
Enter fullscreen mode Exit fullscreen mode
for v1, v2, v3 in [[0, 1, 2], [3, 4, 5]]:
    print(v1, v2, v3)
# 0 1 2
# 3 4 5
Enter fullscreen mode Exit fullscreen mode
for v1, *v2, v3 in [[0, 1, 2, 3, 4, 5], [6, 7, 8, 9, 10, 11]]:
    print(v1, v2, v3)
    print(v1, *v2, v3)
# 0 [1, 2, 3, 4] 5
# 0 1 2 3 4 5
# 6 [7, 8, 9, 10] 11
# 6 7 8 9 10 11
Enter fullscreen mode Exit fullscreen mode
print(*[0, 1], 2, *[3, 4, *[5]])
# 0 1 2 3 4 5
Enter fullscreen mode Exit fullscreen mode
print([*[0, 1], 2, *[3, 4, *[5]]])
# [0, 1, 2, 3, 4, 5]
Enter fullscreen mode Exit fullscreen mode
def func(p1='a', p2='b', p3='c', p4='d', p5='e', p6='f'):
    print(p1, p2, p3, p4, p5, p6)

func()
# a b c d e f

func(*[0, 1, 2, 3], *[4, 5])
# 0 1 2 3 4 5
Enter fullscreen mode Exit fullscreen mode
def func(p1='a', p2='b', *args):
    print(p1, p2, args)
    print(p1, p2, *args)
    print(p1, p2, ['A', 'B', *args, 'C', 'D'])

func()
# a b ()
# a b Nothing
# a b ['A', 'B', 'C', 'D']

func(*[0, 1, 2, 3], *[4, 5])
# 0 1 (2, 3, 4, 5)
# 0 1 2 3 4 5
# 0 1 ['A', 'B', 2, 3, 4, 5, 'C', 'D']
Enter fullscreen mode Exit fullscreen mode

list() can create a list with or without an iterable as shown below:

*Memo:

  • The 1st argument is iterable(Optional-Default:()-Type:Iterable):
    • Don't use iterable=.
# Empty list
print(list())
print(list(()))
# []

print(list([0, 1, 2, 3, 4]))                      # list
print(list((0, 1, 2, 3, 4)))                      # tuple
print(list({0, 1, 2, 3, 4}))                      # set
print(list(frozenset([0, 1, 2, 3, 4])))           # frozenset
print(list(iter([0, 1, 2, 3, 4])))                # iterator
print(list(range(5)))                             # range
# [0, 1, 2, 3, 4]

print(list({'name': 'John', 'age': 36}))          # dict
print(list({'name': 'John', 'age': 36}.keys()))   # dict.keys()
# ['name', 'age']

print(list({'name': 'John', 'age': 36}.values())) # dict.values()
# ['John', 36]

print(list({'name': 'John', 'age': 36}.items()))  # dict.items()
# [('name', 'John'), ('age', 36)]

print(list('Hello'))                              # str
# ['H', 'e', 'l', 'l', 'o']

print(list(b'Hello'))                             # bytes
print(list(bytearray(b'Hello')))                  # bytearray
# [72, 101, 108, 108, 111]
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)