DEV Community

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

Posted on

List in Python (5)

Buy Me a Coffee

*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 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

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

A list can be shallow-copied and deep-copied as shown below:

<Shallow copy>:

*Memo:

  • v1 and v2 refer to different outer lists and the same inner list.
  • is keyword can check if v1 and v2 refer to the same outer and/or inner list.
  • list.copy(), copy.copy(), list() and slicing can shallow-copy a list:
    • list.copy() has no arguments.
import copy

v1 = ['a', 'b', ['c', 'd']]
v2 = v1.copy()
v2 = copy.copy(v1)
v2 = list(v1)
v2 = v1[:]

print(v1) # ['a', 'b', ['c', 'd']]
print(v2) # ['a', 'b', ['c', 'd']]

print(v1 is v2, v1[2] is v2[2])
# False True

v2[1] = 'X'
v2[2][0] = 'Y'
          #       ↓↓↓   ↓↓↓
print(v1) # ['a', 'b', ['Y', 'd']]
print(v2) # ['a', 'X', ['Y', 'd']]
          #       ↑↑↑   ↑↑↑
Enter fullscreen mode Exit fullscreen mode

<Deep copy>:

*Memo:

  • v1 and v2 refer to different outer and inner lists.
  • copy.deepcopy() deep-copies a list.
  • copy.deepcopy() should be used because it's safe, deeply copying a list while list.copy(), copy.copy(), list() and slicing aren't safe, shallowly copying a list.
import copy 

v1 = ['a', 'b', ['c', 'd']]
v2 = copy.deepcopy(v1)

print(v1) # ['a', 'b', ['c', 'd']]
print(v2) # ['a', 'b', ['c', 'd']]

print(v1 is v2, v1[2] is v2[2])
# False False

v2[1] = 'X'
v2[2][0] = 'Y'
          #       ↓↓↓   ↓↓↓
print(v1) # ['a', 'b', ['c', 'd']]
print(v2) # ['a', 'X', ['Y', 'd']]
          #       ↑↑↑   ↑↑↑
Enter fullscreen mode Exit fullscreen mode

Top comments (0)