web-profile

Python unpacking

a, b = (1, 2)

print(a) # 1
print(b) # 2

a, _ = (1, 2) # ignore 2

print(a) # 1

a, b, *c = (1, 2, 3, 4, 5)

print(a) # 1
print(b) # 2
print(c) # [3, 4, 5]

a, b, *_ = (1, 2, 3, 4, 5) # ignore [3, 4, 5]

print(a) # 1
print(b) # 2

a, b, *c, d = (1, 2, 3, 4, 5)

print(a) # 1
print(b) # 2
print(c) # [3, 4]
print(d) # 5

a, b, *_, d = (1, 2, 3, 4, 5) # ignore [3, 4]

print(a) # 1
print(b) # 2
print(d) # 5

Leave a Reply

Your email address will not be published.