×

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

Map Syntax in Python

Introduction: In Python, a function called map acts as an iterator, returning a result after each item in an iterable has been subjected to a function (tuple, lists, etc.). When you...

6 minutes read.

Confusion Matrix Visualization Python

The confusion matrix is a two-dimensional array that compares the anticipated and actual category labels. These are the True Positive, True Negative, False Positive, and False Negative classification categories for...

4 minutes read.

Python Statistical Module

Python Statistical Module The Python statistical module provides various functions that we can use in our Python program, to perform mathematical statistics operations on the numerical data given to us. The...

8 minutes read.

How to run a Program in Python

How to run a Program in Python Writing a program in Python is an easy task, beginners who are ready to kickstart their career in the world of programming can create...

3 minutes read.

Python Tuple count() Method

Python Tuple count() Method The tuple.count() method in Python returns the number of times a specified value appears in the tuple. Syntax tuple.count(value) Parameter value- This parameter represents the element to search in the tuple Return This...

1 minute read.

Python String strip() method

Python String strip() method The string. strip() method in Python removes any leading (spaces at the beginning) and trailing (spaces at the end) characters (space is the default leading character to...

1 minute read.

Python Identifiers

Identifiers in Python User-defined names are identifiers in Python that are used to name variables, functions, classes, modules, and other things. You can create Python identifiers using these rules: As an identifier...

4 minutes read.

Python callable() Function

Python callable() Function The callable() function returns a boolean value ‘True’ if the specified object is callable, else it returns False. Syntax callable(object) Parameter Object:  The object parameter represents the value to test if it is callable...

1 minute read.

Genetic Algorithm in python

Python : Python is an object oriented programming language which is highly interpreted and is highly interactive. Python was created by Guido van Rossum in the year 1985 – 1990 .The source...

4 minutes read.

Python Dictionary clear() method

Python Dictionary clear() method The dictionary.clear() method in Python removes all the elements from a dictionary. Syntax dictionary.clear() Parameter NA Return None Example 1 # Python program explaining # the dictionary.clear() method # initialising the dictionary fruits =...

1 minute read.

Python tuple() function

Python tuple() function The tuple() function in Python creates a tuple object. Syntax tuple([iterable]) Parameter Iterable: This parameter represents a sequence, collection or an iterator object. Return This function returns a tuple object. Example 1 # Python Program explaining #...

1 minute read.

Python BytesIO

Python Programming Language 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...

3 minutes read.

Python List insert() method

Python List insert() method The list.insert () method in Python inserts an item at the specified position. Syntax list.insert(i, x) Parameter i: This parameter represents a number specifying the position to insert the given value. x: This parameter signifies...

1 minute read.

Python Parser

Introduction : Parsing is referred to as the processing and translation of a Python program into machine language. In general, we could indeed say that the command parse is used to...

4 minutes read.

Python Super function

The super function is used to access and call the methods or functions involved in the parent class during Inheritance. What is Inheritance? The process where the parent class inherits some of...

3 minutes read.

Find whether the given stringnumber is palindrome or not

Problem statement Sam is found of playing with strings. One day he thought of finding whether a string is a palindrome or not. He wanted to develop a computer process to...

2 minutes read.

Composition in Python

Composition in Python Composition is one of the important concepts of Object-oriented programming (OOPs). Composition basically enables us for creating complex types objects by combining other types of objects in the...

6 minutes read.

Google Chrome API in Python

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

Python Skyline

Python Programming Language: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a...

3 minutes read.

Google Python Class

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.