We use two operators * (for tuples) and ** (for dictionaries).
Consider a situation where we have a function that receives four arguments. We want to make call to this function and we have a list of size 4 with us that has all arguments for the function. If we simply pass list to the function, the call doesn’t work.
TypeError: fun() takes exactly 4 arguments (1 given)
We can use * to unpack the list so that all elements of it can be passed as different parameters.
(1, 2, 3, 4)
As another example, consider the built-in range() function that expects separate start and stop arguments. If they are not available separately, write the function call with the *-operator to unpack the arguments out of a list or tuple:
When we don’t know how many arguments need to be passed to a python function, we can use Packing to pack all arguments in a tuple.
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 and args 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
Below is an example that shows both packing and unpacking.
(Geeksforgeeks, awesome, world!)
** is used for dictionaries
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)”.
<class 'dict'> language = Python name = geeks ID = 101
Applications and Important Points
1. Used in socket programming to send a vast number of requests to a server.
2. Used in Django framework to send variable arguments to view functions.
3. There are wrapper functions that require us to pass in variable arguments.
4. Modification of arguments become easy, but at the same time validation is not proper, so they must be used with care.
This article is contributed by Shwetanshu Rohatgi .If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
- Unpacking a Tuple in Python
- Python | Unpacking nested tuples
- Python | Unpacking tuple of lists
- Python | Unpacking dictionary keys into tuple
- Default arguments in Python
- Command Line Arguments in Python
- Python: Passing Dictionary as Arguments to Function
- Python | Set 6 (Command Line and Variable Arguments)
- Python | Passing dictionary as keyword arguments
- How to pass multiple arguments to function ?
- Core arguments in serializer fields - Django REST Framework
- MySQL-Connector-Python module in Python
- Python | Index of Non-Zero elements in Python list
- Python | Merge Python key values to list
- Reading Python File-Like Objects from C | Python
Improved By : Jablonski