Open In App

Python List methods

Improve
Improve
Like Article
Like
Save
Share
Report

Python List Methods are the built-in methods in lists used to perform operations on Python lists/arrays.

Below, we’ve explained all the methods you can use with Python lists, for example, append(), copy(), insert(), and more.

List / Array Methods in Python

Let’s look at some different methods for lists in Python:

S.no Method Description
1 append() Used for adding elements to the end of the List. 
2 copy() It returns a shallow copy of a list
3 clear() This method is used for removing all items from the list. 
4 count() These methods count the elements.
5 extend() Adds each element of an iterable to the end of the List
6 index() Returns the lowest index where the element appears. 
7 insert() Inserts a given element at a given index in a list. 
8 pop() Removes and returns the last value from the List or the given index value.
9 remove() Removes a given object from the List. 
10 reverse() Reverses objects of the List in place.
11 sort() Sort a List in ascending, descending, or user-defined order
12 min() Calculates the minimum of all the elements of the List
13 max() Calculates the maximum of all the elements of the List

This article is an extension of the below articles:

Adding Element in List

Let’s look at some built-in Python methods to add element in a list.

1. Python append() method

Adds element to the end of a list.

Syntax: list.append (element)

Example:

Python3




# Adds List Element as value of List.
List = ['Mathematics', 'chemistry', 1997, 2000]
List.append(20544)
print(List)


Output

['Mathematics', 'chemistry', 1997, 2000, 20544]


2. Python insert() method

Inserts an element at the specified position. 

Syntax:

list.insert(<position, element)

Note: The position mentioned should be within the range of List, as in this case between 0 and 4, else wise would throw IndexError. 

Example:

Python3




List = ['Mathematics', 'chemistry', 1997, 2000]
# Insert at index 2 value 10087
List.insert(2, 10087)
print(List)


Output

['Mathematics', 'chemistry', 10087, 1997, 2000]


3. Python extend() method

Adds items of an iterable(list, array, string , etc.) to the end of a list

Syntax: List1.extend(List2)

Example:

Python3




List1 = [1, 2, 3]
List2 = [2, 3, 4, 5]
  
# Add List2 to List1
List1.extend(List2)
print(List1)
  
# Add List1 to List2 now
List2.extend(List1)
print(List2)


Output

[1, 2, 3, 2, 3, 4, 5]
[2, 3, 4, 5, 1, 2, 3, 2, 3, 4, 5]


Important functions of the Python List

We have mentioned some essential Python list functions along with their syntax and example:

1. Python sum() method

Calculates the sum of all the elements of the List. 

Syntax: sum(List)

Example:

Python3




List = [1, 2, 3, 4, 5]
print(sum(List))


Output

15


What happens if a numeric value is not used as a parameter? 

The sum is calculated only for numeric values, else wise throws TypeError. 

See example

Python3




List = ['gfg', 'abc', 3]
print(sum(List))


Output:

Traceback (most recent call last):
File "", line 1, in
sum(List)
TypeError: unsupported operand type(s) for +: 'int' and 'str'


2. Python count() method

Calculates the total occurrence of a given element of the List. 

Syntax: List.count(element)

Example:

Python3




List = [1, 2, 3, 1, 2, 1, 2, 3, 2, 1]
print(List.count(1))


Output

4


3. Python len() method

Calculates the total length of the List. 

Syntax: len(list_name)

Example:

Python3




List = [1, 2, 3, 1, 2, 1, 2, 3, 2, 1]
print(len(List))


Output

10


4. Python index() method

Returns the index of the first occurrence. The start and end indexes are not necessary parameters. 

Syntax: List.index(element[,start[,end]])

Example:

Python3




List = [1, 2, 3, 1, 2, 1, 2, 3, 2, 1]
print(List.index(2))


Output

1


Another example: 

Python3




List = [1, 2, 3, 1, 2, 1, 2, 3, 2, 1]
print(List.index(2, 2))


Output

4


5. Python min() method

Calculates minimum of all the elements of List.

Syntax: min(iterable, *iterables[, key])

Example:

Python3




numbers = [5, 2, 8, 1, 9]
print(min(numbers))


Output

1


6. Python max() method

Calculates the maximum of all the elements of the List.

Syntax: max(iterable, *iterables[, key])

Python3




numbers = [5, 2, 8, 1, 9]
print(max(numbers))


Output

9


7. Python sort() method

Sort the given data structure (both tuple and list) in ascending order.

Key and reverse_flag are not necessary parameter and reverse_flag is set to False if nothing is passed through sorted(). 

Syntax: list.sort([key,[Reverse_flag]])

Example:

Python




List = [2.3, 4.445, 3, 5.33, 1.054, 2.5]
  
#Reverse flag is set True
List.sort(reverse=True
  
#List.sort().reverse(), reverses the sorted list  
print(List)        


Output

[5.33, 4.445, 3, 2.5, 2.3, 1.054]


8. Python reverse() method

reverse() function reverses the order of list.

Syntax: list. reverse()

Example:

Python3




# creating a list
list = [1,2,3,4,5]
#reversing the list
list.reverse()
#printing the list
print(list)


Output

[5, 4, 3, 2, 1]


Deletion of List Elements

To Delete one or more elements, i.e. remove an element, many built-in Python functions can be used, such as pop() & remove() and keywords such as del.

1. Python pop() method

Removes an item from a specific index in a list.

Syntax: list.pop([index])

The index is not a necessary parameter, if not mentioned takes the last index. 

Note: The index must be in the range of the List, elsewise IndexErrors occur. 

Example 1:

Python3




List = [2.3, 4.445, 3, 5.33, 1.054, 2.5]
print(List.pop())


Output

2.5


Example 2:

Python3




List = [2.3, 4.445, 3, 5.33, 1.054, 2.5]
print(List.pop(0))


Output

2.3


2. Python del() method

Deletes an element from the list using it’s index.

Syntax: del list.[index]

Example:

Python3




List = [2.3, 4.445, 3, 5.33, 1.054, 2.5]
del List[0]
print(List)


Output

[4.445, 3, 5.33, 1.054, 2.5]


3. Python remove() method

Removes a specific element using it’s value/name.

Syntax: list.remove(element)

Example :

Python3




List = [2.3, 4.445, 3, 5.33, 1.054, 2.5]
List.remove(3)
print(List)


Output

[2.3, 4.445, 5.33, 1.054, 2.5]


We have discussed all major Python list methods, that one should know to work on list. We have seen how to add and remove elements from list and also perform basic operations like count , sort, reverse.

Hope these Python methods were of help!



Last Updated : 04 Dec, 2023
Like Article
Save Article
Share your thoughts in the comments
Similar Reads