Open In App
Related Articles

List methods in Python

Improve Article
Improve
Save Article
Save
Like Article
Like

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

List Methods in Python

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

This article is an extension of the below articles: Python List List Methods in Python | Set 1 (in, not in, len(), min(), max()…) List Methods in Python | Set 2 (del, remove(), sort(), insert(), pop(), extend()…)

Adding Element in List

Python append()

Used for appending and adding elements to the List. It is used to add elements to the last position of the List in Python

Syntax: list.append (element)

Python3




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

Output:

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

Python insert()

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. 

Python3




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

Output:

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

Python extend()

Adds contents to List2 to the end of List1. 

Syntax: List1.extend(List2)

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

Some Essential Python List Functions and how to use them in List.

Python sum()

Calculates the sum of all the elements of the List. 

Syntax: sum(List)

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'

Python count()

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

Syntax: List.count(element)

Python3




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

Output:

4

Python length

Calculates the total length of the List. 

Syntax: len(list_name)

Python3




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

Output:

10

Python index()

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

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

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

Python min()

Calculates minimum of all the elements of List.

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

Python3




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

Output

1

Python max()

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

sort() and reverse() functions

Python reverse()

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

sorted([list[,key[,Reverse_Flag]]])

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

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]

Deletion of List Elements

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

Python pop()

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

Syntax: list.pop([index])

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

Python3




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

Output:

2.5

Python3




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

Output:

2.3

Python del() 

Element to be deleted is mentioned using list name and index. 

Syntax: del list.[index]

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]

Python remove()

The element to be deleted is mentioned using the list name and element. 

Syntax: list.remove(element)

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]

Last Updated : 31 Mar, 2023
Like Article
Save Article
Related Tutorials