×

How to Pass a list as an Argument in Python

Lists in Python

A list is a datatype in python which is used to store the data in a sequence. The lists are mutable; that is, we can change the values in the lists after their creation, but the strings are immutable; that is, if we want to modify the string, we cannot modify them; we need to create another list to store the modified value. The elements in the list are stored in square brackets [ ], and a comma separates each element. We can access the elements in the list with the help of the indexing method; the first element in the list starts with the index 'zero', and the index of the last element is 'length-1', where length is the size of the list. Python has six built-in sequences, but from all of them, the lists are used for many operations because we can store a list, a tuple and a dictionary inside a list using the concept of a nested list.

Example:

#Creating a list of fruits
fruits = [“mango”, “watermelon”, “papaya”]
#Creating a list of vegetables
vegetables=[“tomato”, “cabbage”. “carrot”]

Accessing the Elements inside a List

We can access the element inside a list by the method of indexing. To access the element inside the list, we must mention the index of an element by calling the list.

Example:

#creating the list
my_list = [‘tuple’ ,‘dictionary’, ‘strings’]
#printing the elements inside a list
print(my_list[0])
print(my_list[2])

Output:

tuple
strings

Built-in Functions in Python

The python programming language consists of many built-in functions to perform various operations. To find the length of the list, python provides the len()function. To add an element to a list, we use the append( )function to add an element to the list at the end. The del( ) function is used to remove the element from the list with the help of the index of the elements. In the same way, the insert( )method is used to insert an element into the list by replacing the existing element with its index's help. The clear() function is used to empty the list; it will remove all the elements from the list.

Example:

#creating a list
my_list = [‘tuple’,‘dictionary’, ‘strings’]
#Finding the length of the list
len(my_list)
#displaying the length of the list
print(len(my_list))
#appending an element into the list
my_list.append(“orange”)
#displaying the updated list
print(my_list)
#deleting the element from the list
delmy_list[1]
#displaying the updated list
print(my_list)
#inserting an element into the list
my_list.insert(1, “apple”)
#displaying the updated list
print(my_list)


#clear the list
my_list.clear()
#displaying the updated list
print(my_list)

Output:

3
[‘tuple’,‘dictionary’, ‘strings’, ‘orange’]
[‘tuple’, ‘strings’, ‘orange’]
[‘tuple’,  ‘apple’, ‘orange’]
[ ]

We can also perform slicing operations on lists, same as slicing a sting with the help of the index of the number

Syntax:

List[ starting index : Final Index : Index Jump]

Parameters:

Starting index: Index of the starting element in the list

Final index: Index of the final element in the sliced list

Index  jump: The number by which the slicing operation must be followed,

Example:

#Creating a list
my-list = [20, 30,19, 22, 33.25]
# Slicing the list
new_list = my_list[1:4]
#displaying the sliced list
print(new_list)

Output:

[30, 19, 22,33]

Passing List as an Argument

The method of passing a list, variable, or any data type as an argument to the function is known as passing an argument. The argument passed to a function will be read in the same data type when we call an argument inside a function. We can call a list inside a function with the help of * to pass a list as an argument inside a list.

Code:

deflistings(a, b)
print(“ arg 1: ”  + str( a ))
print(“arg 2: ”    + str( b ))
list = [“apple” , “animal”]
print(str(list))
listargs(*list)

Output:

[“apple”, “animal”]
arg 1: apple
arg 2: animal

We can also pass a list directly inside a function, and we can print the elements in the list with the help of  for loop:

Example:

deffunc( items ):
fori in items:
print(i )
elements = [“box” , “pen”, “book”]
func(elements)

Output:

box
pen
book

Here we can observe that we can pass the list as an argument directly into a function or pass a list as an argument into the function with the help of  * so that the same list data type will be passed into the function. We can perform any operations on the list.


Related Topics

List Subtract in Python

The List is one of the most unique Data Structures in Python. It is generally used to store multiple values in just one single variable. It is one of the...

4 minutes read.

Python vs PHP

Python Python is a high-level, object-oriented, interpreted language that is used to create independent program and algorithms for a variety of applications. It has a large library support base. In 1990,...

3 minutes read.

Python MySQL

In this article, we are going to learn the following: How to connect Python to MySQL.How to create a new Database.Procedure for connecting the newly created database.Procedure for connecting the already...

7 minutes read.

Python Assert

Python Assert Python provides an assert statement which is used to check the logical expression. If the given logical expression is true, then it precedes for the next line; otherwise, it raises an...

2 minutes read.

Python Line Break

Introduction In this tutorial, you will learn line breaks in python.In Python, the new line character is used to indicate the start of a new line and the end of an...

5 minutes read.

Python Lexicographic Order

Python lexicographic order Before we discuss the lexicographic order in Python, we should understandwhat is lexicographic order and sort according to lexicographic order. Lexicographic order In mathematics, the generalization of the alphabetical order...

5 minutes read.

Pltpcolor in Python

Python: Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It is said...

3 minutes read.

Python Parallel Processing

By performing more jobs concurrently, your software may complete more tasks in a shorter amount of time. These aid in solving major issues. The following subjects will be covered in...

2 minutes read.

How to Define a Function in Python?

What is a Function? In programming, a piece of code that executes a specific task or a group of related operations is known as a function. What does Python Functions Do? If you...

6 minutes read.

IPython Display

A command shell, often known as IPython is a software application that makes an operating system's features accessible to users or other applications. Based on the purpose and specific functioning...

6 minutes read.

Python's Qstandarditemmodel

Python More interactive and user-friendly than any other programming language is Python. Many libraries are used by the python programming language to speed up procedures. Python can be used to develop...

3 minutes read.

Collections in python

In this tutorial, we will see what is collection in python.  Further, we will see in-depth the different kinds of collections in python. Collections Collections in python are the built-in module used...

9 minutes read.

Looping through Data Frame in Python

Iterate over Rows and Columns in Pandas Dataframe This tutorial aims to make us understand what Pandas in Python are, what Data Frame in Python is and its significance, what are...

4 minutes read.

Python String Negative Indexing

This page contains all the information how to use “Negative indexing“ in Python with the help of using slicing. What is an Index? An index is a numeric value, which is assigned...

3 minutes read.

os.rename() method in Python

In Python, the os module provides the capacity to interact with the operating system. The operating system comes under the Python module. In this module, Python provides a specific feature...

3 minutes read.

Convert Float to Int in Python using Pandas

Introduction To play with huge amounts of data, in python we require a tool. The tool which is available in Python is pandas. A panda is an open-source library. It is...

4 minutes read.

Python Data Visualization

In this tutorial, we will understand what data visualization means in python. Further, we will see different methods of visualizing data in python. Data visualization In a non-technical language, it is a...

4 minutes read.

Python Syntax

Python is a strong object-oriented programming language that is simple to learn. Python was created to be a very readable programming language. The syntax of the Python programming language is...

8 minutes read.

__GETITEM__ and __SETITEM__ in Python

These methods are used in assignment operations, unary comparison operations, binary comparison operations and binary operations. These are pre-defined methods that perform many operations on a class instance. Examples like...

3 minutes read.

Python Debugger

In this tutorial, we will understand what is debugging in general. Then we will realize what the python debugger means and what is the method to perform it. Now, let us...

3 minutes read.