List Methods
We'll cover the following
- List methods and code examples
- The list.append(x) method
- The list.extend(iterable) method
- The list.insert(index, i) method
- The list.remove(x) method
- The list.pop(index) method
- The list.clear() method
- The list.index(x) method
- The list.count(x) method
- The list.sort(*, key=None, reverse=False) method
- The list.reverse() method
- The list.copy() method
Python offers a wide range of built-in methods that can be used to operate on lists and efficiently utilize their functionality. The table below covers all the crucial list methods you’ll need to know in order to deal with lists effectively!
Name | Syntax | Explanation |
Append | list.append(x) | Adds element x to the end of the list. |
Extend | list.extend(iterable) | Appends elements from the iterable to the end of the list. |
Insert | list.insert(i, x) | Inserts element x at index i in the list. |
Remove | list.remove(x) | Removes the first occurrence of element x from the list. |
Pop | list.pop([i]) | Removes and returns the element at index i, or the last element if i is not specified. |
Clear | list.clear() | Removes all elements from the list. |
Index | list.index(x) | Returns the index of the first occurrence of element x. |
Count | list.count(x) | Returns the number of occurrences of element x in the list. |
Sort | list.sort(*, key=None, reverse=False) | Sorts the elements of the list in ascending order. |
Reverse | list.reverse() | Reverses the order of elements in the list. |
Copy | list.copy() | Returns a shallow copy of the list. |
List methods and code examples
Let’s take a look at coding snippets for each of the methods mentioned above.
The list.append(x)
method
In the code snippet below, we use the append()
method on python_versions
and add “Python 3.11” to the list.
python_versions = ["Python 2", "Python 3"]python_versions.append("Python 3.11")print(python_versions)
The list.extend(iterable)
method
In this snippet, we utilize the extend()
method on python_modules
to add elements from additional_modules
to the list.
python_modules = ["NumPy", "Pandas"]additional_modules = ["Matplotlib", "scikit-learn"]python_modules.extend(additional_modules)print(python_modules)
The list.insert(index, i)
method
Here, the insert()
method is applied to programming_languages
, inserting "C++"
at index 1.
programming_languages = ["C", "Java", "Python", "Ruby"]programming_languages.insert(1, "C++")print(programming_languages)
The list.remove(x)
method
In the code below, we utilize the remove()
method on frameworks to remove the first occurrence of “Flask”
from the list.
frameworks = ["Django", "Flask", "Pyramid", "Flask"]frameworks.remove("Flask")
The list.pop(index)
method
In the following code snippet, the pop()
method is applied to web_frameworks
, removing the element at index 2 and storing it in removed_framework
.
web_frameworks = ["Django", "Flask", "Pyramid", "FastAPI"]removed_framework = web_frameworks.pop(2)print(web_frameworks)
The list.clear()
method
In the following snippet, the clear()
method is used on scripting_languages
to remove all elements from the list.
scripting_languages = ["Python", "Ruby", "Perl"]scripting_languages.clear()print(scripting_languages)
The list.index(x)
method
This code snippet demonstrates the use of the index()
method on languages to find the index of the first occurrence of “Python”
in the list.
languages = ["C", "C++", "Python", "Java", "Python"]python_index = languages.index("Python")print(python_index)
The list.count(x)
method
In the code below, the count()
method is applied to version_numbers
to count the occurrences of the value 3
.
version_numbers = [2, 3, 3, 3, 3, 3, 3, 3, 3, 3]python3_count = version_numbers.count(3)print(python3_count)
The list.sort(*, key=None, reverse=False)
method
Here, the sort method is used on numeric_versions
to sort the list in ascending order.
numeric_versions = [2.7, 3.0, 3.6, 3.7, 3.8, 3.9]numeric_versions.sort()print(numeric_versions)
The list.reverse()
method
In the following code snippet, the reverse()
method is applied to libraries to reverse the order of its elements.
libraries = ["NumPy", "Pandas", "Matplotlib", "Scikit-Learn"]libraries.reverse()print(libraries)
The list.copy()
method
This snippet demonstrates the use of the copy()
method to create a new list (copied_list
) that is a copy of the original list (original_list
).
original_list = ["Python", "is", "so" , "cool"]copied_list = original_list.copy()print(copied_list)