How do you unpack in python?
Unpacking a TupleWhen we create a tuple, we normally assign values to it. This is called "packing" a tuple: Show But, in Python, we are also allowed to extract the values back into variables. This is called "unpacking": ExampleUnpacking a tuple: fruits = ("apple", "banana", "cherry") (green, yellow, red) = fruits print(green) Try it Yourself » Note: The number of variables must match the number of values in the tuple, if not, you must use an asterisk to collect the remaining values as a list. Using Asterisk*If the number of variables is less than the number of values, you can add an ExampleAssign the rest of the values as a list called "red": fruits = ("apple", "banana", "cherry", "strawberry", "raspberry") (green, yellow, *red) = fruits print(green) Try it Yourself » If the asterisk is added to another variable name than the last, Python will assign values to the variable until the number of values left matches the number of variables left. ExampleAdd a list of values the "tropic" variable: fruits = ("apple", "mango", "papaya", "pineapple", "cherry") (green, *tropic, red) = fruits print(green) Try it Yourself » We use two operators * (for tuples) and ** (for dictionaries). Background Python3
Output : TypeError: fun() takes exactly 4 arguments (1 given) Python3
Output : (1, 2, 3, 4) We need to keep in mind that the no. of arguments must be the same as the length of the list that we are unpacking for the arguments. Python3
Output: Traceback (most recent call last): File "/home/592a8d2a568a0c12061950aa99d6dec3.py", line 10, in As another example, consider the built-in range() function that expects separate start and stops arguments. If they are not available separately, write the function call with the *-operator to unpack the arguments out of a list or tuple: Python3
Packing Python3
Output: 15 30 The above function mySum() does ‘packing’ to pack all the arguments that this method call receives into one single variable. Once we have this ‘packed’ variable, we can do things with it that we would with
a normal tuple. args[0] and args[1] would give you the first and second argument, respectively. Since our tuples are immutable, you can convert the args tuple to a list so you can also modify, delete, and re-arrange items in i. Packing and Unpacking Python3
Output: (Geeksforgeeks, awesome, world!) ** is used for dictionaries Python3
Output: 2 4 10 Here ** unpacked the dictionary used with it, and passed the items in the dictionary as keyword arguments to the function. So writing “fun(1, **d)” was equivalent to writing “fun(1, b=4, c=10)”. Python3
Output
Applications and Important Points
Reference : How do I unpack a string in Python?Method : Using format() + * operator + values()
The * operator is used to unpack and assign. The values are extracted using values().
What is unpack operator in Python?Both * and ** are the operators that perform packing and unpacking in Python. The * operator (quite often associated with args) can be used with any iterable (such as a tuple, list and strings), whereas the ** operator, (quite often associated with kwargs) can only be used on dictionaries.
How do I unpack a parameter in Python?When the arguments are in the form of a dictionary, we can unpack them during the function call using the ** operator. A double asterisk ** is used for unpacking a dictionary and passing it as keyword arguments during the function call.
|