In this article, we will discuss comprehensive notes for python list functions class 11. As we have already completed Creating lists, List manipulations for class 11 in previous sections. Let us start python list functions class 11.
Topics Covered
Python list functions class 11
As you are well aware that python has many library functions that reduced the coder efforts to perform some operations in an efficient manner. These built-in functions can be used for list manipulation as well. In this section of Python list functions class 11, we will discuss them as per your syllabus.
When we declare a list object in python code, this list object is an instance of List class. To use any built-in for list manipulation, you need to write it in this manner:
<listobject>.<function>()
Some of the library functions not required the alias names.
These functions are:
- len()
- list()
- index()
- append()
- extend()
- insert()
- pop()
- clear()
- count()
- remove()
- reverse()
- sort()
- sorted()
Let’s start the python list functions class 11 explanation.
The len() function()
This function returns the length of the list. The length of the list is equal to the number of elements present in the list. It is a standard library function.
Syntax:
len(list_object)
Have a look at the following code:
>>> l = ['Mon','Tue','Wed','Thurs','Fri','Sat']
>>> print(len(l))
The output of the above code is 6, as there are 6 elements specified in the list.
If the list contains the nested list, it will be something like this:
>>> l=[['Mon','Tue','Wed'],['Thu','fri'],'Sat','Sun']
>>> print(len(l))
The output of the above code is 4, as there are 2 sub lists elements and 2 main list elements specified in the list.
The list() method
This function will convert the passed parameters into list. If no arguments passed then it will create an empty list.
Syntax:
list([elements])
Take a look at this code:
>>> list()
It returns [], empty list.
>>> list("TutorialAICSIP")
It will return [‘T’, ‘u’, ‘t’, ‘o’, ‘r’, ‘i’, ‘a’, ‘l’, ‘A’, ‘I’, ‘C’, ‘S’, ‘I’, ‘P’] as output.
rno=int(input("Enter the rollno:"))
stu_name=input("Enter the name:")
fees=int(input("Enter the Fees:"))
stu_rec=list([rno,stu_name,fees])
print(stu_rec)
In the above code, three separate elements merged into a list, using list() method.
Similarly you can convert any tuple, dictionary elements into the list using list() function.
In the next section of python list functions class 11, we will discuss the functions used with the list instance.
The index() method()
This method returns the matched index of element from the list.
Syntax:
<list_obj>.index(<element>)
Observe this code:
>>> l=[11,22,27,34,67,98]
>>> l.index(27)
The above code will return 2 as output. The element value 27 at index 2.
If the number which is not present in the list elements and passed as parameter python will return as error.
>>> l=[11,22,27,34,67,98]
>>> l.index(2)
The above code raise following error:
Traceback (most recent call last):
File "<pyshell#11>", line 1, in <module>
l.index(2)
ValueError: 2 is not in list
The next section explains the functions which are used to insert elements into the list for python list functions class 11.
The append() method
The append() method will add elements into the existing list. The element which is added by append() method will add at the end of the list.
Syntax:
<list_obj>.append(<element>)
Have a look at this code:
l=[]
l.append(10)
print(l)
The append() method exactly takes one argument to add the values. The value can be a scalar value, tuple, list, dictionary, etc.
The extend() method
The extend() method add multiple values into the list.
Syntax:
<list_obj>.extend(<element>)
Observe the following code:
l=[]
l.extend([11,22,33,44])
print(l)
The insert() method
As you learn how to add or insert elements into the list at the end using append() method and extend() method. In this section we will see one more method to insert() element at desired location.
Syntax:
<list_obj>.insert(<idx>,<element>)
The insert() method takes two parameters:
- idx – Index position of the element
- Element – Value of needs to be insert
Observe the following code:
l=['Jan','Mar','Apr']
l.insert(1,'Feb')
print(l)
The output will be:
[‘Jan’, ‘Feb’, ‘Mar’, ‘Apr’]
It can also takes a negative index to be inserted.
The next section of python list functions class 11 explains the functions which are used to remove the elements from the list.
The pop() method
The pop() is used to remove element from the list.
Syntax:
<list_obj>.pop(<idx>)
Look at the following example:
l=['Jan','Feb','Mar','Apr']
l.pop()
print(l)
When the pop() method is used without any parameter, it will delete the last element.
The output will be:
[‘Jan’, ‘Feb’, ‘Mar’]
You can delete a specific element by passing the index value of that particular element.
l=['Jan','Feb','Mar','Apr']
l.pop(2)
print(l)
The output will be:
[‘Jan’, ‘Feb’, ‘Apr’]
The remove() method
The remove method is used to remove the elements by its value. If you are not aware about the index of the element and you know the value, you can use remove method.
Syntax:
<list_obj>.remove(<element_value>)
Observe this code:
l=['Jan','Feb','Mar','Apr']
l.remove('Feb')
print(l)
The output will be:
[‘Jan’, ‘Mar’, ‘Apr’]
The clear() method
It will remove all elements from the list. This function doesn’t require any parameter.
Syntax:
<list_obj>.clear()
l=['Jan','Feb','Mar','Apr']
l.clear()
print(l)
It will return [] i.e. empty list.
In the next section of python list functions class 11, we will discuss some functions with common operations of a list.
The count() method
It will count the presence of element passed as an argument from the list. If the element is not available in the list, it will return zero.
Syntax:
<list_obj>.count(element)
Let us have look at the following code:
l=['Jan','Feb','Mar','Apr','Jan']
l2=l*2
print(l2.count('Jan'))
The reverse() method
It will display the list in the reverse form. It will reverse the list element in place. It doesn’t require any value as parameter or argument.
Syntax:
<list_obj>.reverse()
Look at this code:
l=['Jan','Feb','Mar','Apr']
l.reverse()
print(l)
The sort() method
It sorts the elements in ascending or descending order. The default order is ascending order.
Syntax:
<list_obj>.sort([reverse=False/True])
The code:
l=['Jan','Feb','Mar','Apr']
l.sort()
print(l)
The output will be:
[‘Apr’, ‘Feb’, ‘Jan’, ‘Mar’]
To display the list in descending order use the reverse parameter and pass it with the value “reverse=True”.
Observe this code:
l=['Jan','Feb','Mar','Apr']
l.sort(reverse=True)
print(l)
The output will be:
[‘Mar’, ‘Jan’, ‘Feb’, ‘Apr’]
The next function of python list functions class 11 is a standard library function which is used to sort the results.
The sorted() method
This function sorted() function also sort the list but it will return a new sorted list. Observe the following code:
l=['Jan','Feb','Mar','Apr']
l1=sorted(l)
print(l1)
Moreover you can also use some of the following functions:
- max()
- min()
- sum()
Watch this video lesson:
So I hope you enjoyed the article python list functions class 11. Hit the like button and share this article with your groups and friends.
If you have any specific doubts or queries, feel free to ask in the comment section.
Thank you very much for visiting to our blog.
Download the as PDF.