Built with Tutify.io

    AIStudyLabs

    Python List index() with Examples

    Python List index() with Examples

    The list.index() method in Python is used to check if an element exists in the list or not. This method takes the value as an argument and returns an index of the value in the list. If a value is not found, it returns a ValueError. This method also takes several other parameters, we will discuss different scenarios by considering all parameters with examples.

    1. Only item as a parameter
    2. With Start parameter
    3. With Start and End parameters
    4. index() method with try-except block

    <a id="h-1-quick-examples-of-list-index-method"></a>

    1. Quick Examples of List.index() method

    Let's see how to find the element using index() method in the list.

    
    # Quick Examples
    
    # Consoder list of countries
    countries=["India","China","Japan","Russia","England","Japan"]
    
    # With item as parameter
    print(countries.index('Japan'))
    
    # With start as parameter
    print(countries.index('Japan',4))
    
    # With start and stop parameters
    print(countries.index('China',3,8))
    
    # Handle index() method
    try:
       print(countries.index('Italy'))
    except:
      print("Italy not exists !")
    

    <a id="h-2-python-list-index-method"></a>

    2. Python List index() method

    The Python list.index() method will return the index position of the given element that exists in the list. If the element is not found, ValueError is raised. To overcome this error, we can specify this method inside the try-except block.

    It will take two optional parameters along with the item. The start parameter specifies from which position in the list the search has to start. and the end parameter specifies till what position in the list the search has to end.

    <a id="h-2-1-list-index-syntax"></a>

    2.1 List index() Syntax

    Let's look at the syntax of the list index() method

    
    # Here, mylist1 is the input list.
    mylist1.index(item,Start,Stop)
    

    <a id="h-2-2-list-index-parameters"></a>

    2.2 List.index() Parameters

    Let's discuss the parameters that we can pass to this method.

    By default, the search starts from starting position till the end of the list.

    <a id="h-2-3-list-index-return"></a>

    2.3 List.index() Return

    It will return the index position of the element if the element is found. Otherwise, ValueError is returned.

    <a id="h-3-python-index-examples"></a>

    3. Python Index Examples

    Let's discuss some examples to understand this method better.

    <a id="h-3-1-finding-index-of-a-value-in-python-list"></a>

    3.1 Finding Index of a Value in Python List

    Let's create a list of countries and return the "Japan" index position. In the below example, we can see that "Japan" exists in the list hence, the index is returned as 2. Note that in Python the index starts from 0.

    
    # Consoder list of countries
    countries=["India","China","Japan","Russia","England"]
    
    # With item as parameter
    print(countries.index('Japan'))
    
    # Output:
    # 2
    

    <a id="h-3-2-with-start-parameter"></a>

    3.2 With Start parameter

    If you have the item present in the list at several positions, by default it returns the index of first position from the python list. By using the start position param, you can specify from where you need to specify the search. Let's create a list of countries and return the "Japan" index position after position 3.

    
    # Consoder list of countries
    countries=["India","China","Japan","Russia","England","Japan","Russia"]
    
    # With start parameter
    print(countries.index('Japan',3))
    
    # Output:
    # 5
    

    We specified the start position as 3, So the index() method will start searching from this position. The Japan presents at 2 and 5 position, since we started at 4 it return the position as 5.

    <a id="h-3-3-with-start-amp-end-parameter"></a>

    3.3 With Start & End parameter

    Use the end param to specify at which position to stop looking for an element. Let's create a list of countries and return the "China" index position from position 3 to position 8.

    
    # Consoder list of countries
    countries=["India","China","Japan","Russia","England","Japan","Russia","China"]
    
    # With start and stop parameters
    print(countries.index('China',3,8))
    
    # Output:
    # 7
    

    Here, we specified the start position as 3 and the end position as 8, So the index() method will start searching from this position till the 7th position. It returns the index 7 as the value China is present in that position

    <a id="h-3-4-no-element-case"></a>

    3.4 No element Case

    As mentioned above, if the element is not present in the python list the index() method returns a ValueError. Let's create a list of countries and return the "Italy" index position.

    
    # Consoder list of countries
    countries=["India","China","Russia","China"]
    
    # No element case
    print(countries.index('Italy'))
    

    Returns below error as the element Italy not present in the list.

    To handle this error, we can use the try-except block.

    
    # Consoder list of countries
    countries=["India","China","Japan","Russia","China"]
    
    # Use try-except
    try:
       print(countries.index('Italy'))
    except:
      print("Italy not exists !")
    
    # Output:
    # Italy not exists !
    

    Now the Error Message is handled.

    <a id="h-4-conclusion"></a>

    4. Conclusion

    In this article, you have learned the syntax of Python list index method, its parameters and using this parameters to get the index position of the element present in the list. For each parameter, we explained examples to understand this method much better. Also, we saw how to handle the error if the element is not present in the list.

    <a id="h-related-articles"></a>

    Related Articles