Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

python unpacking

"""Pythonic Unpacking
"""
## Unpacking iterables
lst = [1, 2, 3]
a, b, c = lst
print(a, b, c)
# a = 1, b = 2, c = 3

## The use of * for unpacking iterables
v, *end = [1, 2, 3, 4]
print(v, end)
# v = 1, end = [2, 3, 4]

v, *middle, z = [1, 2, 3, 4]
print(v, middle, z)
# v = 1, middle = [2, 3], z = 4

*v, end = [1, 2, 3, 4]
print(v, end)
# v = [1, 2, 3], end = 4


"""Try out other iterables to unpack!"""
Source by builtin.com #
 
PREVIOUS NEXT
Tagged: #python #unpacking
ADD COMMENT
Topic
Name
9+2 =