Blog Post #53: Essential List Methods: index(), count(), and reverse()

In the last few posts, we’ve learned how to add (Post #50), remove (Post #51), and sort (Post #52) the items in our lists. Python’s lists come equipped with several other handy utility methods for finding information about their contents.

In this post, we’ll cover three of these essential methods: .index() to find an item’s position, .count() to see how many times an item appears, and .reverse() to flip the order of the list.

.index(): Finding an Item’s Position

If you need to know the index of a specific item in a list, you can use the .index() method. It searches the list for the given value and returns the index of the first match.

The syntax is: my_list.index(value)

letters = ['a', 'b', 'c', 'd', 'b', 'e']

# Find the index of the first 'b' in the list
index_of_b = letters.index('b')

print(f"The first 'b' is at index: {index_of_b}")

The output will be The first 'b' is at index: 1.

Note that it only returns the index of the first occurrence. The second ‘b’ at index 4 is ignored. Just like the .remove() method, if you try to find the index of a value that is not in the list, Python will raise a ValueError.

.count(): Counting Occurrences

The .count() method is a simple way to find out how many times a specific value appears anywhere in a list.

The syntax is: my_list.count(value)

letters = ['a', 'b', 'c', 'd', 'b', 'e']

# Count how many times 'b' appears
num_of_bs = letters.count('b')
print(f"The letter 'b' appears {num_of_bs} times.")

# Count a letter that is not in the list
num_of_zs = letters.count('z')
print(f"The letter 'z' appears {num_of_zs} times.")

The output shows that it works even for items not in the list:

The letter 'b' appears 2 times.
The letter 'z' appears 0 times.

.reverse(): Reversing a List In-Place

The .reverse() method does exactly what its name implies: it reverses the order of the elements in a list.

Crucially, just like the .sort() method, .reverse() modifies the list in-place and returns None. Do not make the common mistake of assigning the result of this method to a new variable, as that variable will end up with the value None.

numbers = [1, 2, 3, 4, 5]
print(f"Original order: {numbers}")

# This reverses the 'numbers' list directly
numbers.reverse()

print(f"Reversed order: {numbers}")

The output shows the original list has been permanently changed:

Original order: [1, 2, 3, 4, 5]
Reversed order: [5, 4, 3, 2, 1]

.reverse() vs. Slicing

This in-place modification is different from the slicing technique my_list[::-1] that we saw in Post #48. The slice creates a new reversed list while leaving the original untouched. The .reverse() method modifies the original list. Choose the one that fits your needs.

original_list = [10, 20, 30]

# Method 1: Slicing creates a new list
new_reversed_list = original_list[::-1]
print(f"The new reversed list is: {new_reversed_list}")
print(f"The original is still unchanged: {original_list}")

What’s Next?

You now have several more tools for inspecting and manipulating your lists. You can find an item’s location with .index(), count its frequency with .count(), and reverse the list in-place with .reverse().

In our discussion of .reverse(), we touched on a very important topic: the difference between modifying a list in-place and creating a new copy. This distinction is one of the most critical concepts to understand when working with mutable objects like lists. In Post #54, we will take a deep dive into how to properly copy a list and why a simple = assignment can lead to unexpected bugs.

Author

Debjeet Bhowmik

Experienced Cloud & DevOps Engineer with hands-on experience in AWS, GCP, Terraform, Ansible, ELK, Docker, Git, GitLab, Python, PowerShell, Shell, and theoretical knowledge on Azure, Kubernetes & Jenkins. In my free time, I write blogs on ckdbtech.com

Leave a Comment