Unpack-Anywhere-With-Star
Apr 24, 2022 - ⧖ 1 minUnpacking iterables in python with *
is a pretty handy trick for writing code that is just a tiny bit more pythonic than not.
arr: Tuple[Union[int, str]] = (1, 2, 3, 'a', 'b', 'c')
print(arr)
>>> (1, 2, 3, 'a', 'b', 'c')
# the * unpacks the tuple into the individual elements
print(*arr)
>>> 1, 2, 3, 'a', 'b', 'c'
x, y, z, *alphas = arr
# x = 1, y = 2, z = 3
# alphas = [ 'a', 'b', 'c' ]
But @Ned Batchelder showed me via Twitter than you can arbitrarily unpack arguments based on position - it doesn't have to be done at the beginning or the end!
x, y, *mixed, alpha = arr
# x = 1, y = 2
# mixed = [3, 'a', 'b']
# alpha = 'c'
I'm not entirely sure when I'll need this but it definitley shows me another example of how flexible python is!