Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

Python Find in List: How to Find the Index of an Element in List

  • 03 Feb, 2025
  • Com 0
Python Find in List

A list is an ordered collection of elements, and each element has an index starting from 0. So, the first element has an index of 0, the second has 1, the third has 2, and so on.

Here are three ways to find the index of an element in a Python List:

  1. Using list.index()
  2. Using enumerate() and list comprehension
  3. Using for loop

Method 1: Using list.index()

The index() function returns the index of the element’s first occurrence, regardless of whether the element appears twice.

If the value you are looking for does not appear in the list, it will return a ValueError. So, we need to handle the ValueError exception.

Visual Representation

Using list index() function to find an index of an element in the Python list

The above figure shows that we want to find the index of element 19. Since indexing starts with 0 in the list, element 19 appears to be located at index 2.

Example

main_list = [11, 21, 19, 46]

element_to_find = 19

try:
    index = main_list.index(element_to_find)
    print(f"{element_to_find} found at index {index}")
except ValueError:
    print(f"{element_to_find} not found in the list")

# Output: 19 found at index 2

Now, if element 19 appears twice in the list, it will still return the first index of the element because it returns only a single index, not a list of multiple indices.

Index() function return only first occurence of the list

main_list = [11, 21, 19, 46, 19]

element_to_find = 19

try:
    index = main_list.index(element_to_find)
    print(f"{element_to_find} found at index {index}")
except ValueError:
    print(f"{element_to_find} not found in the list")

# 19 found at index 2

In the above code, you can see that element “19” occurs at positions 2 and 4, but in the output, we found only index 2 because index() will stop searching after index 2.

Pros

  1. It works efficiently for the first occurrence.
  2. The index() is a built-in method. No extra package is required.

Cons

  1. It does not return all occurrences for duplicates.
  2. By default, it requires error-handling mechanisms like try/catch. You can use the “in” operator to avoid it.

Using the “in” operator to avoid try…catch

To avoid potential errors related to the existence of input value, we can use the “in” operator before using the “index()” method.

main_list = [11, 21, 19, 46]

element_to_find = 50

if element_to_find in main_list:
    index = main_list.index(element_to_find)
    print(f"{element_to_find} found at index {index}")

else:
    print(f"{element_to_find} not found in the list")

# Output: 50 not found in the list

Method 2: Using enumerate() and list comprehension

If the same element appears multiple times in the list and you want to find all the indices of multiple occurrences, you can use list comprehension with enumerate() function.

Visual Representation

Using enumerate() and list comprehension

The above figure shows that we have to find the index of element 19 in the list. As we can see, element 19 is located twice in the list, index 2 and index 4, so it returns 2 and 4 as a list.

Example

main_list = [11, 21, 19, 46, 19]

# Element to find
element_to_find = 19

# Use list comprehension with enumerate() to find indices
indices = [index for index, item in enumerate(
    main_list) if item == element_to_find]

if indices:
    print(f"{element_to_find} found at indices {indices}")
else:
    print(f"{element_to_find} not found in the list")

# Output: 19 found at indices [2, 4]

Pros

  1. It finds all indices, so it handles duplicates gracefully.
  2. The syntax is readable and concise. Perfect pythonic way.

Cons

  1. This approach can be an overkill if you only need the first occurrence.
  2. It creates a list of all indices upfront (inefficient for huge lists with many matches).

Method 3: Using for loop with enumerate()

You can use a for loop with the enumerate() function to iterate through a list for a specific element and provide its index if it is found.

Example

main_list = [11, 21, 19, 46]

# Element to find
element_to_find = 19

# Iterate through the list with a for loop
for index, element in enumerate(main_list):
    if element == element_to_find:
        print(f"{element_to_find} found at index {index}")
        break  # Stop the loop after finding the first occurrence

else:
    print(f"{element_to_find} not found in the list")

# Output: 19 found at index 2

Pros

  1. It provides a clean way to handle if the element is not found in the list.
  2. You don’t need to use try…except blocks to handle the case where the element is absent.

Cons

  1. It is not ideal for multiple occurrences.
  2. It is less pythonic.
  3. It is less flexible than list comprehension.

That’s all!

Post Views: 33
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

How to Print List without Brackets in Python
How to Append a String to Another in Python

Leave a Reply Cancel reply

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

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend