Python List Methods – Tutorial with Examples

A list is a data structure in Python that is used to store a collection of items. Lists are mutable, meaning that the elements of a list can be changed after the list has been created. Python provides several built-in methods for working with lists, which makes it easier to manipulate and process the elements in a list.

Methods for Python Lists

Below is a list of the most commonly used methods for Python lists:

We’ve tried to explain the usage of each of these Python List methods with an example. If you want to explore more with even more detailed syntax and examples, we’ve already individual articles published on our website for each of them. You can simply click on the heading links to redirect to those articles.

append()

append() is a method that is used to add an item to the end of a list. The item can be of any data type.

fruits = ['apple', 'banana', 'cherry']
fruits.append('orange')
print(fruits)

Output: ['apple', 'banana', 'cherry', 'orange']

clear()

clear() is a method that is used to remove all the elements from a list.

fruits = ['apple', 'banana', 'cherry']
fruits.clear()
print(fruits)

Output: []

copy()

copy() is a method that is used to create a shallow copy of a list. A shallow copy is a copy of the list that references the same objects as the original list, instead of creating new objects with the same values as the original list.

fruits = ['apple', 'banana', 'cherry']
fruits_copy = fruits.copy()
print(fruits_copy)

Output: ['apple', 'banana', 'cherry']

count()

count() is a method that is used to count the number of occurrences of an item in a list.

fruits = ['apple', 'banana', 'cherry', 'apple']
count = fruits.count('apple')
print(count)

Output: 2

extend()

extend() is a method that is used to add multiple items to the end of a list. The items can be of any data type, and they can be added in the form of a list or any other iterable object.

fruits = ['apple', 'banana', 'cherry']
new_fruits = ['orange', 'grapes']
fruits.extend(new_fruits)
print(fruits)

Output: ['apple', 'banana', 'cherry', 'orange', 'grapes']

index()

index() is a method that is used to find the index of an item in a list. If the item is not found in the list, a ValueError is raised.

fruits = ['apple', 'banana', 'cherry']
index = fruits.index('banana')
print(index)

Output: 1

insert()

insert() is a method that is used to insert an item at a specific position in a list. The first argument is the index of the position where the item should be inserted, and the second argument is the item itself.

fruits = ['apple', 'banana', 'cherry']
fruits.insert(1, 'orange')
print(fruits)

Output: ['apple', 'orange', 'banana', 'cherry']

pop()

pop() is a method that is used to remove an item from a list. By default, the item that is removed is the last item in the list. If an index is specified as an argument, the item at that index will be removed.

fruits = ['apple', 'banana', 'cherry']
last_item = fruits.pop()
print(fruits)
print(last_item)

Output:

['apple', 'banana']
cherry

remove()

remove() is a method that is used to remove the first occurrence of an item in a list. If the item is not found in the list, a ValueError is raised.

fruits = ['apple', 'banana', 'cherry', 'apple']
fruits.remove('apple')
print(fruits)

Output: ['banana', 'cherry', 'apple']

reverse()

reverse() is a method that is used to reverse the order of the items in a list. The original list is modified, and no copy is created.

fruits = ['apple', 'banana', 'cherry', 'apple']
fruits.reverse()
print(fruits)

Output: ['apple', 'cherry', 'banana', 'apple']

sort()

sort() is a method that is used to sort the items in a list in ascending order. By default, the sort method sorts the elements of the list in place, meaning that the original list is modified, and no copy is created. You can also sort a list in descending order by passing the argument reverse=True to the sort() method.

fruits = ['apple', 'banana', 'cherry']
fruits.sort()
print(fruits)

Output: ['apple', 'banana', 'cherry']

fruits = ['apple', 'banana', 'cherry']
fruits.sort(reverse=True)
print(fruits)

Output: ['cherry', 'banana', 'apple']

It is important to note that the sort() method can only be used on lists containing elements of the same data type. If a list contains elements of different data types, a TypeError will be raised.

It is also worth mentioning that in addition to the built-in methods for lists, you can use many other functions and operations from the Python Standard Library, such as len(), min(), max(), sum(), and so on, to manipulate and process lists. There are also many other data structures available in Python, such as tuples, sets, dictionaries, and more, each with their own specific use cases and features.

In conclusion, lists are an essential and versatile data structure in Python, and the built-in methods and functions provide powerful and convenient tools for working with lists and manipulating the data they store.

Leave a Reply

Your email address will not be published. Required fields are marked *