Signup/Sign In

Find a given element's index in Python List

Posted in Programming   LAST UPDATED: AUGUST 31, 2021

    The problem statement for this article is "How to find a specific element's index in a python list?" and we will be discussing multiple different ways to do this with simple and easy to understand code examples.

    Consider we have a list of values and a searchable value, which we have to find in the list, there are many methods which can be used to find an element's index in a list in python. A few of them are listed below, other approaches would be appreciated in the comment section.


    Using Python list index() function

    This function takes in the element to be searched and optional parameters include the start and the end index so that the search can be limited to a specific subset of the list elements. This function returns the index of the element that is being searched.

    Using the start and end index parameters would reduce the computation time while searching for an element in a large list.

    Syntax:

    list_name.index(element_to_be_searched, start_index, end_index)

    Time for an Example:

    
    list_elements = [1,2,3,4,5]
    # search for 2 in the above list
    print(list_elements.index(2))
    
    # new list with 100 elements
    new_list = list(range(0,100))
    # search for 5 in the list starting from index 10
    print(new_list.index(5, 10, 20))
    
    

    Output:

    1
    
    Traceback (most recent call last):
      File "/tmp/sessions/3f846ff078a4ac18/main.py", line 8, in <module>
        print(new_list.index(5, 10, 20))
    ValueError: 5 is not in list

    After defining the list elements, the index function is used and the element to be searched is passed as an argument to the index function. The first call to the index function returns 1, i.e the element 2 is at 1st index. While in case of the second call, we have provided the start and end index, and the element to be searched is not found between the provided index values, hence we get an error.


    Using the enumerate function

    This function can be used to iterate over the list getting the index and and value of the element. Let's take a quick example:

    
    list_elements = [1,2,3,4,5]
    item_to_be_searched = 2
    for index, item in enumerate(list_elements):
        if item == item_to_be_searched:
            print(item, index)
            break
    
    

    In the code above a list of elements has been defined. This list is traversed using the enumerate keyword and an if condition has been put which finds out the element to be searched. After this, the element, along with its index are printed.


    Using the in operator

    If the idea is to just find whether the element is present or not, then the in operator could be used. This returns a Boolean value, i.e True if the element is present in the list and False if the element is not present in the list. For example,

    
    list_elements = [1,2,3,4,5]
    # if 4 is present in list
    print(4 in list_elements)
    
    

    Output:

    True

    In the code above after the list has been defined, the operator in is used to iteratively check if an element is present in the list. This returns true since the element 4 is present in the list.


    Using lambda function

    Following is the code example,

    
    # defining a function to find index in a list using lambda
    get_indexes = lambda x, searchable: [i for (y, i) in zip(searchable, range(len(searchable))) if x == y]
    
    # using the above function to search for elements
    print(get_indexes(2, [1, 2, 3, 4, 5]))
    
    print(get_indexes('f', 'ajvhdfhgkjdnsd'))
    
    

    In the code above, the zip function takes iterable attributes and the lambda function, which is an anonymous function (a function that doesn’t have a name to it), takes the arguments and matches the value that we search for. It returns the index of the element that is being searched in the list.


    Conclusion

    There are many different ways to accomplish the solve a problem in python. I would suggest to use the one which is more pythonic in nature, which means less code and more output. Well, for this problem, you can use any one of the above stated solution. If you know some other way to search for an element's index in python, do share it with us.

    You may also like:

    About the author:
    I love writing about Python and have more than 5 years of professional experience in Python development. I like sharing about various standard libraries in Python and other Python Modules.
    Tags:PythonProgrammingPython List
    IF YOU LIKE IT, THEN SHARE IT
     

    RELATED POSTS