Open In App

Python List methods

Last Updated : 18 Apr, 2024
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 Python list methods you can use with Python lists, for example, append(), copy(), insert(), and more.

List Methods in Python

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

S.noMethodDescription
1append()Used for 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 an 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:

Adding Element in List in Python

Let’s look at some built-in list functions in Python 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: 

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]


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.

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 functions, 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 using list Python Methods.

Hope these Python methods were of help!



Similar Reads

List Methods in Python | Set 1 (in, not in, len(), min(), max()...)
List methods are discussed in this article. 1. len() :- This function returns the length of list. List = [1, 2, 3, 1, 2, 1, 2, 3, 2, 1] print(len(List)) Output: 10 2. min() :- This function returns the minimum element of list. List = [2.3, 4.445, 3, 5.33, 1.054, 2.5] print(min(List)) Output: 1.054 3. max() :- This function returns the maximum eleme
2 min read
Advanced Python List Methods and Techniques
Lists are just like dynamically sized arrays, declared in other languages (vector in C++ and ArrayList in Java). Lists need not be homogeneous always which makes it a most powerful tool in Python. A single list may contain DataTypes like Integers, Strings, as well as Objects. Lists are mutable, and hence, they can be altered even after their creati
6 min read
List Methods in Python | Set 2 (del, remove(), sort(), insert(), pop(), extend()...)
Some of the list methods are mentioned in set 1 below List Methods in Python | Set 1 (in, not in, len(), min(), max()…) More methods are discussed in this article. 1. del[a : b] :- This method deletes all the elements in range starting from index 'a' till 'b' mentioned in arguments. 2. pop() :- This method deletes the element at the position mentio
4 min read
How Do I Get List Of Methods In A Python Class?
Effective Python programming and debugging depend on a grasp of the methods that are accessible in a class. You may investigate the features offered by a Python class by obtaining a list of its methods. The ideas and procedures required to get a list of methods within a Python class will be shown to you in this article. In this article, we will see
3 min read
Python | Convert list of string to list of list
Many times, we come over the dumped data that is found in the string format and we require it to be represented in the actual list format in which it was actually found. This kind of problem of converting a list represented in string format back to la ist to perform tasks is quite common in web development. Let's discuss certain ways in which this
7 min read
Python | Convert list of tuples to list of list
This is a quite simple problem but can have a good amount of application due to certain constraints of Python language. Because tuples are immutable, they are not easy to process whereas lists are always a better option while processing. Let's discuss certain ways in which we can convert a list of tuples to list of list. Method #1: Using list compr
8 min read
Python | Convert List of String List to String List
Sometimes while working in Python, we can have problems of the interconversion of data. This article talks about the conversion of list of List Strings to joined string list. Let's discuss certain ways in which this task can be performed. Method #1 : Using map() + generator expression + join() + isdigit() This task can be performed using a combinat
6 min read
Python String Methods | Set 1 (find, rfind, startwith, endwith, islower, isupper, lower, upper, swapcase &amp; title)
Some of the string basics have been covered in the below articles Strings Part-1 Strings Part-2 The important string methods will be discussed in this article1. find("string", beg, end) :- This function is used to find the position of the substring within a string.It takes 3 arguments, substring , starting index( by default 0) and ending index( by
4 min read
Python String Methods | Set 2 (len, count, center, ljust, rjust, isalpha, isalnum, isspace & join)
Some of the string methods are covered in the set 3 below String Methods Part- 1 More methods are discussed in this article 1. len() :- This function returns the length of the string. 2. count("string", beg, end) :- This function counts the occurrence of mentioned substring in whole string. This function takes 3 arguments, substring, beginning posi
4 min read
Python String Methods | Set 3 (strip, lstrip, rstrip, min, max, maketrans, translate, replace &amp; expandtabs())
Some of the string methods are covered in the below sets.String Methods Part- 1 String Methods Part- 2More methods are discussed in this article1. strip():- This method is used to delete all the leading and trailing characters mentioned in its argument.2. lstrip():- This method is used to delete all the leading characters mentioned in its argument.
4 min read
Practice Tags :
three90RightbarBannerImg