Lists

Lists as Arguments

In Python, we can easily expand the list, tuple, dictionary as well as we can pass each element to the function as arguments by the addition of * to list or tuple and ** to dictionary while calling function. We will discuss lists as arguments in this article.

lists as arguments

In Python we can perform all these functions:

Expand the list and tuple with *

  • With default arguments
  • With the variable-length arguments

Expand the dictionary with **

  • With default arguments
  • With the variable-length arguments

Have a look at the following articles for the basic use of Python functions, default arguments, and the variable-length arguments with * and ** while defining functions:

  • Define and call functions in Python (def, return)
  • Default arguments in Python
  • Variable-length arguments (args, *kwargs) in Python
  • In the following sample code available below, lists are used, but the similar applies to the tuples.
  • If the amount of the elements is not matching the number of the arguments, ‘TypeError’ will arise.
                    

# func(*['one', 'two'])

# TypeError: func() missing 1 required positional argument: 'arg3'

# func(*['one', 'two', 'three', 'four'])

# TypeError: func() takes three positional arguments but four were given

Browse more Topics under Lists

With Default Arguments

If the function is having the default arguments, the default arguments will come into a useful step if the number of the elements is not as much as required. If there are numerous elements, ‘TypeError’ will arise.

                    

def func_default(arg1=1, arg2=2, arg3=3):

print(arg1)

print(arg2)

print(arg3)

func_default(*['one', 'two'])

# one

# two

# 3

func_default(*['one'])

# one

# 2

# 3

# func_default(*['one', 'two', 'three', 'four'])

# TypeError: func_default() takes from 0 to 3 positional arguments but 4 were given.

With variable-length arguments

If the function is having a variable-length argument (*args), all the elements after the positional argument will be sent to the variable-length argument.

                    

def func_args(arg1, *args):

print(arg1)

for arg in args:

print(arg)

func_args(*['one', 'two'])

# one

# two

func_args(*['one', 'two', 'three'])

# one

# two

# three

func_args(*['one', 'two', 'three', 'four'])

# one

# two

# three

# four

Expand the dictionary with **

While specifying a dictionary with ** as an argument, the key will be extended as an argument name and value as the argument’s value. Each and every single element will pass as the keyword arguments. Let’s learn more about lists as arguments.

                    

def func(arg1, arg2, arg3):

print(arg1)

print(arg2)

print(arg3)

d = {'arg1': 'one', 'arg2': 'two', 'arg3': 'three'}

func(**d)

# one

# two

# three

func(**{'arg1': 'one', 'arg2': 'two', 'arg3': 'three'})

# one

# two

# three

If there is no key that matches the argument name, or if there is a key that does not match the argument name, TypeError will occur.

# func(**{'arg1': 'one', 'arg2': 'two'})

# TypeError: func() missing 1 required positional argument: 'arg3'

# func(**{'arg1': 'one', 'arg2': 'two', 'arg3': 'three', 'arg4': 'four'})

# TypeError: func() got an unexpected keyword argument 'arg4'

FAQs on Lists as Arguments

Question 1: What is the use of the list () in Python?

Answer: The list() function makes a list object. Moreover, a list object is basically a group that is well-ordered and variable.

Question 2: What does “* *” mean?

Answer: “**” takes a (dict) and extracts its substances and passes them as the parameters to a function. Have a look at this function for instance:

def func(a=1, b=2, c=3): print ‘a’ print ‘b’ print ‘b‘.

Now, we can normally call this function func(1, 2, 3).

Question 3: Describe, how do we pass a list by reference?

Answer: In pass by reference we pass the variable(the bucket) into the function straight. Moreover, the variable acts like a Package that comprises of (the objects) which are said to be its content. However, in the code present above, both ‘list’, as well as ‘my_list’, are similar container variable.

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.