This post was last Updated on February 22, 2023 by Himanshu Tyagi to reflect the accuracy and up-to-date information on the page.
This tutorial will teach the most common Python list methods with syntax and examples.
A list is a built-in data type used to store multiple data items in a single variable. The list is ordered mutable and heterogeneous data structure. A list in Python is created by enclosing data items in square brackets.
Items in the list are stored in an ordered way. They can be extracted using the index, i.e., the element’s position. Items/elements in a list are mutable. We can perform modifications to a defined list. i.e., insertion, deletion, updation, etc.
Items in the list are of type heterogeneous. A list accepts items/elements of different types.
Below is a simple program to create a list of elements.
programming_lang=['C','Java','Python'] print(programming_lang)
Output
[‘C’, ‘Java’, ‘Python’]
Also Read: How to Get User Input in Python
Python List Methods With Syntax and Examples
S. No. | Method | Description |
1. | index() | Returns the first occurrence of a specific element in a list. |
2. | max() | Get the maximum value in a list. |
3. | min() | Fetch the minimum value in a list. |
4. | len() | Count the total number of items in a list. |
5. | insert() | Add an element to a specific index in a list. |
6. | append() | Add an element at the end of a list. |
7. | extend() | Insert elements at the end of a list. |
8. | pop() | Removes an element at the specified index position. |
9. | remove() | Removes the first occurrence of a specified element. |
10. | clear() | Remove all elements from a list. |
11. | sort() | Sorts all list items in ascending order. |
12. | reverse() | Reverse all elements in a list. |
13. | count() | Returns the count of occurrences of a specified element in a list. |
14. | copy() | Copy the content of a list to a new list. |
Also Read: Python filenotfounderror — How to fix filenotfounderror in Python
The below-mentioned methods are the built-in list of methods available in Python.
1. index() method
The index() method returns the first appearance of a specified element in the list.
Code:
programming_lang=['C','Java','Python'] print(programming_lang.index('Python'))
Output:
2
Note: Index starts from 0 in the list. So the element “Python” is present in 3rd position and index value = position-1. Hence index method returns 2.
Also Read: How To Open a File in Python
2. max() method
The max() method returns the maximum value present in a list.
Code:
numbers=[1,2,10,20,5] print(max(numbers))
Output:
1
Also Read: Understand Python Lambda Functions With Examples
3. min() method
The min() method returns the minimum value present in a list.
Code:
numbers=[1,2,10,20,5] print(min(numbers))
Output:
1
Also Read: Python Zip() Function Tutorial With Code Examples
4. len() method
The len() method returns the total number of elements in the list.
Code:
numbers=[1,2,10,20] print(len(numbers))
Output:
4
Also Read: Quicksort Implementation in Python
5. insert() method
The insert() method adds the element to the list at the required/specified index.
Code:
numbers=[1,2,10,20] numbers.insert(2,5) print(numbers)
Output:
[1, 2, 5, 10, 20]
Also Read: How To Create a Python Web Server
6. append() method
The append() method adds the specified element at the end of the list.
Code:
numbers=[1,2,10,20] numbers.append(5) print(numbers)
Output:
[1, 2, 10, 20, 5]
Also Read: 15 Most Common Python Array Programs
7. extend() method
The extend() method adds the multiple elements specified at the end of the list.
Code:
numbers=[1,2,10,20] numbers.extend([3,30]) print(numbers)
Output:
[1, 2, 10, 20, 3, 30]
Also Read: Pattern Programs in Python [Star and Alphabetical Patterns]
8. pop() method
The pop() method removes the element at the specified index position. If the index position is not specified by default, the pop() method removes the last element and returns the popped-out element.
Code:
numbers=[1,2,10,20] numbers.pop(2) print(numbers) numbers.pop() print(numbers)
Output:
[1, 2, 20]
[1, 2]
Also Read: Graph Plotting in Python Using Matplotlib
9. remove() method
The remove() method removes the first occurrence of a specified element.
Code:
numbers=[1,2,10,20] numbers.remove(10) print(numbers)
Output:
[1, 2, 20]
Also Read: Intro to Threading in Python [Understand With Examples]
10. clear() method
The clear() method removes all elements from the list.
Code:
numbers=[1,2,10,20] numbers.clear() print(numbers)
Output:
[]
Also Read: Understanding How Print Statements in Python Work
11. sort() method
The sort() method sorts the elements in the list. By default, it sorts the elements in the list in ascending order. The syntax of sort() method is-
listName.sort(reverse=True|False)
Code:
numbers=[1,20,10,2] #Ascending order numbers.sort() print(numbers) #descending order numbers.sort(reverse=True) print(numbers)
Output:
[1, 2, 10, 20]
[20, 10, 2, 1]
Also Read: How to Create a Neural Network in Python
12. reverse() method
The reverse() method reverses the elements in the list.
Code:
numbers=[1,20,10,2] # reverse the elements in list numbers.reverse() print(numbers)
Output:
[2, 10, 20, 1]
Also Read: Or Operator in Python
13. count() method
The count() method returns the count of occurrences of a specified element in the list.
Code
names=['Himanshu','Akhil','Himanshu'] # find the occurrences of string Himanshu print(names.count('Himanshu'))
Output
2
Also Read: Natural Language Processing in Python: Techniques
14. copy() method
The copy() method copies the content in the list and is used to make a new list with the copied content.
Code
names=['Himanshu','Akhil','Himanshu'] copied_names=names.copy() print(copied_names)
Output
[‘Himanshu’, ‘Akhil’, ‘Himanshu’]
The above-mentioned methods are a few practical built-in methods while processing the list’s data.