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.