List Methods In Python

In the previous tutorials, we’ve looked at how to use the lists in python and how it can store multiple items inside it, but we didn’t explore its methods which are built-in functions in python that can be applied to the list and extend its functionalities. This article will help you learn these methods and make your work more productive.

1. Append() Method

Let’s start with the list’s first and most used ones, which is the append() method responsible for adding new items to the list. If it is empty or already has data, it can add them. See this example:

# Create a List
List = [100, 200, 300]

# Add an Item
List.append(400)

# Print The List
print(List)

When you execute the code, you can see it now has another item at the end, the value 400, after adding it to the list using the append() method. It will always add the new items at the end of your list.

2. Insert() Method

The append() method always adds the items at the end of every list, but sometimes you need to add the items at a specific position, so the insert() method will fix this issue. Let’s see an example:

# Create a List
List = [100, 200, 300]

# Insert an Item 
List.insert(1, 400)

# Print The List
print(List)

When you execute the previous code, you can see that the value 400 has been added in the second position after the 100 value. We’ve specified the position at index 1, which is the second item since the index count in python start from 0. If you’ve put out of range index, it will add it to the last of it.

3. Extend() Method

Sometimes you have two lists in your code, and you need to combine them. If you do this manually, it will be a daunting task. Let’s do this action with the extend() method in the list:

# Create Two Lists
List = [100, 200, 300]
List2 = [400, 500, 600]

# Extend The First List
List.extend(List2)

# Print The List
print(List)

We wanted to add the second list after the first one, so we’ve applied the extend() method on the first list and told it to add the second one to the first one.

4. Clear() Method

From the name, you can understand the benefits of this method. It will clear or remove all of the items inside a given list. Let’s see this example:

# Create a List
List = [100, 200, 300]

# Clear The Items
List.clear()

# Print The List
print(List)

When you execute the code, it will give you empty brackets, which means the list is now empty of items.

5. Sort() Method

Many python developers still don’t know how to sort the list using this method, and they create programs that have a for loop and the if statement to sort the list. Fortunately, python has a built-in method inside the list to do this action:

# Create a List
List = [130, 10, 40]

# Sort The List
List.sort()

# Print The List
print(List)

The sort() method will return the sorted list. The default sort is in ascending order, and you can change this to be in descending order. See this example:

# Create a List
List = [130, 10, 40]

# Sort The List
List.sort(reverse=True)

# Print The List
print(List)

You change the order by only specifying the reverse parameter inside the sort() function and making it True, which will change the sorting order to descending.

6. Index() Method

In some situations in your code, you need to know the index of a particular item in your list to remove it or replace it if an action happens, and more. You can easily do this by a list method called index(). Let’s see an example:

# Create a List
List = [130, 10, 40]

# Get The Item Index
x = List.index(10)

# Print The Item Index
print(x)

It will give you the value 1. As a result, the number 10 is on index number 1, basically the second item since index counting starts from 0.

7. Count() Method

This method is simple, and it will return the number of times an element appears in the list. Let’s try a simple example:

# Create a List
List = [130, 10, 40, 10, 33, 10]

# Count The Item
x = List.count(10)

# Print The Item Count
print(x)

You will get the number 3 as a result which means the value 10 is repeated three times inside that list.

8. Sum() Method

This simple method will help you sum all of the items on the list, and many developers don’t know these practical methods that will make you productive in your job instead of making a whole code to sum all of the items inside it. Let’s see an example:

# Create a List
List = [130, 10, 40, 10, 33, 10]

# Sum List Items
print(sum(List))

You will have the value 233 because it will sum up all of the values inside this array.

9. Min() & Max() Methods

You can also apply the min() function to your list and get the minimum value. This method is used a lot in python programs. Let’s see an example:

# Create a List
List = [130, 40, 10, 33]

# Print The Minimum Value
print(min(List))

You need to apply the method directly on your list, and it will give you the value 10, which is the smallest value. Let’s try the max() method, which will give you the maximum value:

# Create a List
List = [130, 40, 10, 33]

# Print The Minimum Value
print(max(List))

You will also get the maximum value which is the value 130.

Conclusion

Thanks for reading! I hope you enjoyed the article, and there are a lot of other list methods that will help you make your work easy and more productive instead of writing a whole code to perform a specific task.

Scroll to Top