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…


This content originally appeared on DEV Community and was authored by Super Kai (Kazuya Ito)

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
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
for v1, v2, v3 in [[0, 1, 2], [3, 4, 5]]:
    print(v1, v2, v3)
# 0 1 2
# 3 4 5
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
print(*[0, 1], 2, *[3, 4, *[5]])
# 0 1 2 3 4 5
print([*[0, 1], 2, *[3, 4, *[5]]])
# [0, 1, 2, 3, 4, 5]
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
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']

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']

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']]
          #       ↑↑↑   ↑↑↑

<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']]
          #       ↑↑↑   ↑↑↑


This content originally appeared on DEV Community and was authored by Super Kai (Kazuya Ito)


Print Share Comment Cite Upload Translate Updates
APA

Super Kai (Kazuya Ito) | Sciencx (2025-10-06T21:14:11+00:00) List in Python (5). Retrieved from https://www.scien.cx/2025/10/06/list-in-python-5/

MLA
" » List in Python (5)." Super Kai (Kazuya Ito) | Sciencx - Monday October 6, 2025, https://www.scien.cx/2025/10/06/list-in-python-5/
HARVARD
Super Kai (Kazuya Ito) | Sciencx Monday October 6, 2025 » List in Python (5)., viewed ,<https://www.scien.cx/2025/10/06/list-in-python-5/>
VANCOUVER
Super Kai (Kazuya Ito) | Sciencx - » List in Python (5). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/10/06/list-in-python-5/
CHICAGO
" » List in Python (5)." Super Kai (Kazuya Ito) | Sciencx - Accessed . https://www.scien.cx/2025/10/06/list-in-python-5/
IEEE
" » List in Python (5)." Super Kai (Kazuya Ito) | Sciencx [Online]. Available: https://www.scien.cx/2025/10/06/list-in-python-5/. [Accessed: ]
rf:citation
» List in Python (5) | Super Kai (Kazuya Ito) | Sciencx | https://www.scien.cx/2025/10/06/list-in-python-5/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.