Unpack elements in a list or tuple to function arguments
1)Args(*)
In Python function definitions, the special syntax *args is used to pass a variable number of arguments to a function. It is used to pass a variable-length, non-keyworded argument list.
To take in a variable number of arguments, the syntax is to use the symbol *; by convention, it is often used with the word args.
*args enables you to accept more arguments than the number of formal arguments you previously defined. With *args, you can add any number of extra arguments to your existing formal parameters (including zero extra arguments).
For instance, suppose we want to create a multiply function that can take any number of arguments and multiply them all together. It is possible.
Example:
i) Unpacking a list
Below is the implementation:
# givenlist givenlist = ['hello', 'this', 'is', 'BTechGeeks'] # unpack the list print(*givenlist)
Output:
hello this is BTechGeeks
ii)Unpacking a tuple
Below is the implementation:
# giventuple giventuple = ('hello', 'this', 'is', 'BTechGeeks') # unpack the tuple print(*giventuple)
Output:
hello this is BTechGeeks
However, we must ensure that the elements of the list or tuple are exactly equal to function parameters. Otherwise, it will result in an error. As a result, it is commonly used with functions that accept variable length arguments.
Below is the implementation:
def calculateSum(*args): # Arguments of variable length are accepted by this function. num = len(args) if num == 0: return 0 sumargs = 0 for element in args: sumargs += element return sumargs list1 = [1, 2, 3] # passing list to calculateSum function print(calculateSum(*list1)) list2 = [1, 2, 3, 4, 5, 6, 7] # passing list to calculateSum function print(calculateSum(*list2))
Output:
6 28
2)Kwargs(**)
In Python function definitions, the special syntax **kwargs is used to pass a keyworded, variable-length argument list. We call ourselves kwargs with a double star. The reason for this is that the double star allows us to pass keyword arguments through (and any number of them).
** is another symbol provided by Python. When it is prefixed with a dictionary, all of the key value pairs in the dictionary are unpacked to function arguments.
Examples:
1)
Below is the implementation:
def printDict(price, name, Model): print("price =", price) print("name =", name) print("Model =", Model) # given dictionary dictionary = {'price': 100, 'name': 'Apple', 'Model': '12pro'} # printing dictionary arguments using kwargs** printDict(**dictionary)
Output:
price = 100 name = Apple Model = 12pro
Related Programs:
- python how to get current date and time or timestamp
- python how to sort a dictionary by key or value
- python args how to pass multiple arguments to function
- how to reverse a 1d 2d numpy array using np flip and operator in python
- passing variable arguments to a function in java using varargs tutorial and example
- how to scrape amazon data using python scrapy
- how to code a scraping bot with selenium and python