Search code snippets, questions, articles...

Arbitrary Arguments *args in python

def favourite_fruit(*args):
    print("My favourite fruit is : " + args[1])

favourite_fruit("Apple", "Mango", "Banana")
Best JSON Validator, JSON Tree Viewer, JSON Beautifier at same place. Check how cool is the tool
My favourite fruit is Mango

If you do not know how many parameters will be passed to a function then you can use arbitrary arguments in Python. In the code snippet, we have passed three fruits names and want to print the second argument passed as favorite fruit.

Here we have passed these arguments as a tuple so you can access them using its index inside the function code block.

Was this helpful?

You can also pass keyword arguments as much as you want and access them using key name inside the function code block.

def employee_info(**info):
    print("Employee full name is : " + info['firstname'] + ' ' + info['lastname'])

employee_info(firstname="John", lastname="Deo")