Python List Methods

The function len() is useful to find the number of elements in a list. We can use this function as:

n = len(list)

Here, 'n' indicates the number of elements of the list. Similar to len() function, we have max() function that returns biggest element in the list. Also, the min() function returns the smallest element in the list. Other than these function, there are various other methods provided by Python to perform various operations on lists.

List methods and details

Method Example Description
sum() list.sum() Returns sum of all elements in the list.
index() list.index(x) Returns the first occurrenceof x in the list
append() list.append(x) Appends x at the end of the list
insert() list.insert(i,x) Inserts x in to the list in the position specified by i
copy() list.copy() copies all the list elements into a new list and returns it
extend() list.extend(list1) Appends list 1 to list
count() list.count(x) Returns number of occurences of x in the list
remove() list.remove(x) Removes x from the list
pop() list.pop() Removes the ending element from the list
sort() list.sort() Sorts the elements of the list into ascending order
reverse() list.reverse() Reverses the sequence of elements in the list
clear() list.clear() Deletes all elements from the list

list methods example

A Python program to understand list processing methods.

CopiedCopy Code

num = [10,20,30,40,50] 
n = len(num) 
print('No. of elements in num:', n) 
num.append(60) 
print('num after appending 60:', num) 
num.insert(0,5) 
print('num after inserting 5 at 0th position:', num) 
num1 = num.copy() 
print('Newly created list num1:', num1) 
num.extend(num1) 
print('num after appending num1:', num) 
n = num.count(50) 
print('No. of times 50 found in the list num:', n) 
num.remove(50) 
print('num after removing 50:', num) 
num.pop() 
print('num after removing ending element:', num) 
num.sort() 
print('num after sorting:', num) 
num.reverse() 
print('num after reversing:',num) 
num.clear() print('num after removing all elements:', num)