Tuple unpacking with *variable notation
It is fairly common to see tuple unpacking in function with arguments specified as *args and **kwargs. In the example below we look at tuple unpacking in a different setting beyond function arguments functions.
Suppose we have a range of numbers and wish to only retain the first and second as individual variables and the rest into one variable, tuple unpacking is the right tool for this
#!/usr/bin/env python first, second, *remaining = range(7) print("The first value is:", first, "The second value is:", second) print("The remaining values are:", remaining)
The output is:
The first value is: 0 The second value is: 1 The remaining values are: [2, 3, 4, 5, 6]
Notice that the output of the remaining range are stored in a list which can iterated and manipulated.