In this tutorial, I will discuss about all the built-in methods for lists in python.
Built-in Methods for Lists
Python provides special built-in methods for list objects. So let’s see each of these methods and use.
append( )
The append()
methods takes a python object as parameter andd appends that object to end of the list. This method does not split the collection objects but instead appends the complete collection as one element to the end of the list. It takes only one object as parameter and appends that as one element to the list.
Syntax of append()
method is :
list.append( value )
parameter : The append()
method takes any python object as a paramaster. It takes only one parameter and appends that as an element to the end.
return : The append()
method does not return anything but it modifies the existing list by adding the parameter as an element to the end of the list.
Example :
list1 = [1,2,3,4]
print("Original list : ",list1)
list1.append(5)
print("After appending : ",list1)
list1.append([2.4,86])
print("Again after appending : ",list1)
Original list : [1, 2, 3, 4]
After appending : [1, 2, 3, 4, 5]
Again after appending : [1, 2, 3, 4, 5, [2.4, 86]]
extend( )
The extend()
methods takes an iterable python object as parameter and appends all the end of the list. This method splits the iterable object and appends all the elements of that object to the end of the list. It takes only one iterable object as parameter and appends all elements from that object to end of the list.
Syntax of extend()
method is :
list.extend(iterable)
parameter : The extend()
method takes an iterable python object as a parameter and appends all elements from that object to end of the list.
return : The extend()
method does not return anything but it modifies the existing list by adding the elementd of the parameter to the end of the list.
Example :
list2 = ['a','b','c','d']
print("Original list : ",list2)
list2.extend(('e','f','g'))
print("After extending : ",list2)
list2.extend({'h':'m','j':'n'})
print("Again after extending : ",list2)
Original list : ['a', 'b', 'c', 'd']
After extending : ['a', 'b', 'c', 'd', 'e', 'f', 'g']
Again after extending : ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j']
insert( )
The insert()
methods takes an index value as a paramater and takes a python object to insert at that particular position. As we know, in python indexing starts from 0 so we can give index value from 0 onwards. If the index value is out of bound for the list then it will add one element to the end of the list for the first time and from next time it keeps changing the last element for out of bound index values.
Syntax of insert()
method is :
list.insert(index, object)
parameter : The insert()
method takes an index value as position and a python object as another parameter which
will be added at the position specified.
return : The insert()
method does not return anything but it modifies the existing list by inserting the given python object at the given index.
Example :
list3 = [1,'a',2,'b',3,4,5]
print("Original list : ",list3)
list3.insert(5,'c')
print("After inserting a value : ",list3 )
list3.insert(10,'e')
print("Again after inserting a value : ",list3 )
Original list : [1, 'a', 2, 'b', 3, 4, 5]
After inserting a value : [1, 'a', 2, 'b', 3, 'c', 4, 5]
Again after inserting a value : [1, 'a', 2, 'b', 3, 'c', 4, 5, 'e']
remove( )
The remove()
methods takes a value as a paramater and removes first occurence of that value from the list. If the value is not present in the list then it throws ValueError.
Syntax of remove()
method is :
list.remove( )
parameter : The remove()
method takes a value as a parameter and removes first occerence of that value if it present in the list.
return : The remove()
method does not return anything but it modifies the existing list by removing the given value from the list and if the value is not present in the list then it throws ValueError.
Example :
list4 = [1,2,3,4,5,1,2,3]
print("Original list : ", list4)
list4.remove(2)
print("After removing 2 : ",list4)
list4.remove(8) #Throws Value Error
Original list : [1, 2, 3, 4, 5, 1, 2, 3]
After removing 2 : [1, 3, 4, 5, 1, 2, 3]
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
in
3 list4.remove(2)
4 print("After removing 2 : ",list4)
----> 5 list4.remove(8) #Throws Value Error
ValueError: list.remove(x): x not in list
pop( )
The pop()
method removes and returns an item from the list. It removes the last element from the list by default. It does not have any parameter but it has an optional parameter of index from where the element is removed. If the index value is out of range or the list is empty then the method throws IndexError.
Syntax of pop()
method is :
list.pop(index(optional))
parameter : The pop( ) method does not have any paramater but it has an optional parameter for the index value of the element which we want to remove.
return : The pop( ) method returns the value which is removed from the given position or last element of the list and it modifies the existing list by removing the given value from the list and if the value is not present in the list then it throws ValueError.
Example :
print("Original list : ",list4)
print("Element : ",list4.pop(), ", List after pop() : ",list4)
print("Element : ",list4.pop(3), ", List after pop(3) : ",list4)
Original list : [1, 3, 4, 5, 1, 2, 3]
Element : 3 , List after pop() : [1, 3, 4, 5, 1, 2]
Element : 5 , List after pop() : [1, 3, 4, 1, 2]
clear( )
The clear()
method removes all the elements from the list and make it an empty list.
Syntax of clear()
method is :
list.clear( )
parameter : The clear()
method does not take any paramater.
return : The clear()
method does not return anything but it removes all the elements from the list and makes it an empty list.
Example :
print("Original list : ",list3)
list3.clear()
print("After clear() : ",list3)
Original list : [1, 'a', 2, 'b', 3, 'c', 4, 5, 'e']
After clear() : []
index( )
The index()
method returns index or position of first occurence of the value given as parameter. If the value is not present in the list then it raises ValueError.
Syntax of index()
method is :
list.index(value)
parameter : The index()
method takes a value as a paramater whose index or position we want to get.
return : The index()
method returns the index or position of the value given as paramater. If the value is not found in the list then it throws ValueError.
Example :
list5 = [1,2,['a','b'],3,'c',4]
print("Original List : ",list5)
print("Index of 2 : ",list5.index(2))
print("list5[1] = ",list5[1])
print("Index of ['a','b'] : ",list5.index(['a','b']))
print("list5[2] = ",list5[2])
print("Index of 'd' : ",list5.index('d'))
Original List : [1, 2, ['a', 'b'], 3, 'c', 4]
Index of 2 : 1
list5[1] = 2
Index of ['a','b'] : 2
list5[2] = ['a', 'b']
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
in
5 print("Index of ['a','b'] : ",list5.index(['a','b']))
6 print("list5[2] = ",list5[2])
----> 7 print("Index of 'd' : ",list5.index('d'))
ValueError: 'd' is not in list
count( )
The count()
method returns the number of occurences of the value given as parameter in the list.
Syntax of count()
method is :
list.count( value )
parameter : The count()
method takes a value as a paramater for which we want to get the number of occurrences in the list.
return : The count()
method returns the number of occurrences of the value given as parameter. If the value is not present in the list then it returns 0.
Example :
list6 = [1,4,7,9,2,4,1,5,7,9,1,3,7,44]
print("Original list : ",list6)
print("Number of occurrences of 7 : ",list6.count(7))
print("Number of occurrences of 6 : ",list6.count(6))
Original list : [1, 4, 7, 9, 2, 4, 1, 5, 7, 9, 1, 3, 7, 44]
Number of occurrences of 7 : 3
Number of occurrences of 6 : 0
sort( )
The sort()
method sorts a list inplace in ascending order by default. If we want to sort the list in desscending order them there is an optional paramater reverse it has default value of False, when we give value True to reverse then it will sort the list in descending order.
Syntax of sort()
method is :
list.sort(reverse = True/False)
parameter : The sort()
method does nnot take any parameter but it has an optional parameter reverse to sort the list in descending order.
return : The sort()
method does not return anything but it modifies the list and arranges the elements into ascending order by default.
Example :
list7 = [7,3,54,21,9,3,2,7,5,3,6,8,0,1,3,5,1,5,9]
print("Original list : ",list7)
list7.sort()
print("After sort (default) : ",list7)
list7.sort(reverse = True)
print("After 'reverse = True' in sort : ",list7)
Original list : [7, 3, 54, 21, 9, 3, 2, 7, 5, 3, 6, 8, 0, 1, 3, 5, 1, 5, 9]
After sort (default) : [0, 1, 1, 2, 3, 3, 3, 3, 5, 5, 5, 6, 7, 7, 8, 9, 9, 21, 54]
After 'reverse = True' in sort : [54, 21, 9, 9, 8, 7, 7, 6, 5, 5, 5, 3, 3, 3, 3, 2, 1, 1, 0]
reverse( )
The reverse()
method reverses the order of the elements of the list.
Syntax of reverse()
method is :
list.reverse( )
parameter : The reverse()
method does not take any parameter.
return : The reverse()
method does not return anything but it modifies the list and arranges the elements in reverese order.
Example :
list8 = [2,'a','c',1,4,'d',3,5,'c']
print("Original list : ",list8)
list8.reverse()
print("After reverse : ",list8)
Original list : [2, 'a', 'c', 1, 4, 'd', 3, 5, 'c']
After reverse : ['c', 5, 3, 'd', 4, 1, 'c', 'a', 2]
copy( )
The copy()
method is used to get a true copy of the list. It copies the original list into a new list and if we update any of the list then it will not affect other list.
Syntax of copy()
method is :
List1 = list.copy( )
parameter : The copy()
method does not take any parameter.
return : The copy()
method returns a true copy of the list which means that both the lists are independent.
Example :
list1 = [1,2,3,4,5]
list2 = list1.copy()
print("list1 = ",list1)
print("list2 = ",list2)
list1[2] = 10
print("After updating list1")
print("list1 = ",list1)
print("list2 = ",list2)
list1 = [1, 2, 3, 4, 5]
list2 = [1, 2, 3, 4, 5]
After updating list1
list1 = [1, 2, 10, 4, 5]
list2 = [1, 2, 3, 4, 5]
Conclusion
In this tutorial we have seen all built-in methods for list objects in python with examples.
If you have any questions please comment below, also share your views and suggestions in the comment box.