Table of Contents
List methods in python
Python supports various compound datatypes like List, tuple,dictionary etc.
In previous post we studied about how lists are mutable and what are list operations?
In this post we will be concentrating on various list methods.
List consist of various methods for easy functioning and utilization of python script.
Many list methods like list(), append(), remove() pop() len() etc are used to provide various functionalities and can be used again and again as per need without need of coding again this helps in reducing redundancy.
List of list methods
- append()
- It appends an element at the end of the list.
L.append(ele)
- count()
- It returns the number of occurrences of value in the list.
L.count(ele)
- clear()
- It clears all the elements or values present in the list without deleting entire list.
L.clear()
- copy()
- It copies elements from one list to another list.
- List mentioned in parenthesis is copied in original list.
L.copy(L2)
- extend()
- It extends list by appending elements from the iterable.
- In short we can add more than one value in the end of the original list by using extend method.
L.extend(L2)
- index()
- It return first index of value and raises ValueError if the value is not present.
L.index(value, [start, [stop]])
- insert()
- It inserts new element on desired index in the given list.
- When the value is added on given index, it will not replace existing value by deleting it instead it will get added to the desired location by shifting remaining values further.
L.insert(index, object)
- pop()
- It adds new element in the end of the given list if no parameters are mentioned.
- If we provide the parameter i.e. index value , it will delete the value from given index.
L.pop()
OR
L.pop(index)
- remove()
- It removes first occurrence of value and raises ValueError if the value is not present.
- NOTE: value mentioned in remove() is not the index position it is the actual value.
- In case of repetitive values the first occurrence of value will be deleted.
L.remove(value)
- reverse()
- It reverses the given list.
L.reverse()
- sort()
- It sorts the given list in ascending order by default.
- If we wish to sort in ascending order we can provide a parameter in sort() called as reverse.
- By default reverse = False, if we want result in descending order we must set reverse=True
L.sort() #ascending order
L.sort(reverse=True) #descending order
- len()
- It returns the length of the list
L.len()
- list()
- It is used for type casting i.e. it is used to convert other datatypes to list datatype.
list(seq)
- max()
- It is used to find max value from the list
max(L)
- min()
- It is used to find min value from the list
min(L)
Example of list methods
#LIST METHODS IN PYTHON (itvoyagers.in)
var1=['a','python',2,6.3]
print('var1 is',var1)
var2=[6,5,4,'l','program']
print('var2 is',var2)
print("n ")
#length of list
print('len(var2)',len(var2))
print('len(var1)',len(var1))
print("n ")
#type function
print('type(var2)',type(var2))
print('type(var1)',type(var1))
print("n ")
#appending values
var1.append(5)
print(' after appending value in var1',var1)
var1.insert(2,'new')
print('after inserting value in var1',var1)
var1.extend(var2)
print('after extinding iterable in var1',var1)
print("n ")
#count
x=var1.count(5)
print("count of var1",x)
print("n ")
#copy
var5=var1.copy()
print("var5",var5)
print("n ")
#reverse
var1.reverse()
print('reverse of var1 is',var1)
print("n ")
#sorting
var3=[2,5,6,7,8,1,0,9]
print('befor sorting var3',var3)
var3.sort() #for ascending order
print('sorted var3 in ascending order',var3)
var3.sort(reverse=True) #for descending order
print('sorted var3 in descending order',var3)
print("n ")
#repetition operator
print("var1*3",var1*3)
print("n ")
#concatenation
print("var1+var3",var1+var3)
print("n ")
#clear values
var5.clear() #clears all elements but does not delete list
print("Cleared var5",var5)
print("n ")
#deleting values from list
print("var1 before del statement",var1)
del var1[3] #del statement
print('var1',var1)
var2.remove(5) #remove() takes value as parameter
print('after removing value from var2',var2)
var2.pop() #removes last element from list
print('after poping last value from var2',var2)
var2.pop(1) #pop() can take index as parameter
print('after poping value from given index in var2',var2)
Output of list methods in python

Other python related post
Other advance python related post
We are aiming to explain all concepts of Python in easiest terms as possible.

ITVoyagers
Author