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: 

In this example, we are using index() method which is one of the list functions in Python, searching the first occurrence of the element 2, starting from index 2 in the list.

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])

Example:

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]


Python List methods

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

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

Similar Reads

List Methods in Python

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

Adding Element in List in Python

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

Important Functions of the Python List

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

Deletion of List Elements

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

Contact Us