In this article, you’ll learn the Python list methods. Python lists are an important and useful data structure. Being able to manipulate them effectively allows you to be more efficient in your programming and make debugging simpler. If you’re looking to expand your knowledge of lists, here are 11 list methods or list functions that you can use in Python to work with lists more efficiently and effectively. I highly recommend you get the “Python Crash Course Book” to learn Python.
List Methods | Description |
list.append() | The append method is used to add an element at the end of the list. |
list.clear() | The clear method is used to delete all the elements of a list. |
list.copy() | The copy method is used to copy the list. |
list.count() | The count method is used to count any element in a list. |
list.extend() | The extend method is used to add multiple elements at the end of a list using a tuple or a list. |
list.index() | The index method is used to find the index of an element. |
list.insert() | The insert method is used to insert a new element into a specific index of a list. |
list.pop() | The pop method is used to delete an element using its index. |
list.remove() | The remove method is used to delete an element using its value. |
list.reverse() | The reverse method is used to reverse all the elements of a list. |
list.sort() | The sort method is used to sort the elements in ascending or descending order. |
1. Append Method
In this example, I will append 70 to the list. You can further explore the append method using this article.
list = [100, 30, 10, 50, 80, 50] list.append(70) print(list)
Output:
[100, 30, 10, 50, 80, 70]
2. Clear Method
In this example, I will delete all the elements of the list using clear method. You can further explore the clear method using this article.
list = [100, 30, 10, 50, 80, 50] list.clear() print(list)
Output:
[]
3. Copy Method in List
In this example, I will copy the list and save it in a new variable and then display a new variable.
list = [100, 30, 10, 50, 80, 50] list2 = list.copy() print(list2)
Output:
[100, 30, 10, 50, 80]
4. Count Method in List
In this example, I will count 50 in the list. You can further explore the count method using this article.
list = [100, 30, 10, 50, 80, 50] print(list.count(50))
Output:
2
5. Extend Method in List
In this example, I will extend the list by adding another list. You can further explore the extend method using this article.
list = [100, 30, 10, 50, 80, 50] list.extend([12, 14 , 8]) print(list)
Output:
[100, 30, 10, 50, 80, 50, 12, 14, 8]
6. Index Method in List
In this example, I will display the index of 50. This method shows the index of first occurrence of the element. You can further explore the index method using this article.
list = [100, 30, 10, 50, 80, 50] print(list.index(50))
Output:
3
7. Insert Method in List
In this example, I will insert 70 at index 2. You can further explore the insert method using this article.
list = [100, 30, 10, 50, 80, 50] list.insert(2, 70) print(list)
Output:
[100, 30, 70, 10, 50, 80, 50]
8. Pop Method in List
The pop method is used to delete the specific index. If the index is not mentioned, it will delete the last element of the list.
list = [100, 30, 10, 50, 80, 50] list.pop(2) print(list)
Output:
[100, 30, 50, 80, 50]
list = [100, 30, 10, 50, 80, 50] list.pop() print(list)
Output:
[100, 30, 10, 50, 80]
9. Remove Method in List
In this example, I will remove 50 from a list but it will remove the first occurrence only.
list = [100, 30, 10, 50, 80, 50] list.remove(50) print(list)
Output:
[100, 30, 10, 80, 50]
10. Reverse Method in List
In this example, I will use reverse method to reverse all the element of a list.
list = [100, 30, 10, 50, 80, 50] list.reverse() print(list)
Output:
[50, 80, 50, 10, 30, 100]
11. Sort Method in List
In the following examples, I will sort the list in ascending and descending order.
list = [100, 30, 10, 50, 80, 50] list.sort() print(list)
Output:
[10, 30, 50, 50, 80, 100]
list = [100, 30, 10, 50, 80, 50] list.sort(reverse=True) print(list)
Output:
[100, 80, 50, 50, 30, 10]