Built with Tutify.io

    AIStudyLabs

    Python Tuple index() Method

    Python Tuple index() Method

    The Tuples index() function in Python is used to find the index of the first occurrence of a specified element in the tuple. If the element is not found in the tuple, a ValueError will be raised. In this article, I will explain index() syntax and usage with tuple examples.

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

    1. Quick Examples of Tuple index() Method

    If you are in a hurry, below are some quick examples of the tuple index() method.

    
    # Quick examples of tuple index() method
    
    # Example 1: Use the tuple index() method
    # Get an index of an element
    tuples = ('Pyspark', 'Hadoop', 'Java', 'Spark')
    indx = tuples.index('Java')
    
    # Example 2: Use the tuple index() method
    tuples = (1, 3, 5, 7, 9, 11)
    indx = tuples.index(7)
    
    # Example 3: Get an index from multiple occurrences
    tuples = ('Spark', 'Hadoop', 'Java', 'Hadoop')
    indx = tuples.index('Hadoop', 2)
    
    # Example 4: Use tuple index() function to slice notation
    tuples = ('Spark', 'Hadoop', 'Java', 'Hadoop', 'Pyspark')
    indx = tuples.index('Hadoop', 0, 3)
    
    # Example 5: ValueError in tuple index() method
    tuples = ('Pyspark', 'Hadoop', 'Java', 'Spark')
    indx = tuples.index('Pandas')
    

    <a id="h-2-syntax-of-tuple-index-method"></a>

    2. Syntax of tuple index() Method

    Following is a syntax of the index() method.

    
    # Syntax of tuple index() method
    tuple.index(value, start, end)
    

    2.1 Parameters of tuple index()

    <a id="h-2-2-return-value"></a>

    2.2 Return Value

    This method returns the index of the first occurrence of the specified value. If the value is not found, a ValueError is raised.

    <a id="h-3-use-the-tuple-index-method-to-get-an-index-of-an-element"></a>

    3. Use the Tuple index() Method to Get an Index of an Element

    To get the index of an element in a Python tuple use the index() method. In the below example, tuples is a tuple object containing the elements 'Pyspark', 'Hadoop', 'Java', and 'Spark'. You can use the index() method to find the index of the second occurrence of the element which is'Java' in the tuple.

    
    # Use the tuple index() method
    # Get an index of an element
    tuples = ('Pyspark', 'Hadoop', 'Java', 'Spark')
    indx = tuples.index('Java')
    print('Index of Java is', indx)
    
    # Output:
    # Index of Java is 2
    

    Here's another example of a tuple containing the elements 1, 3, 5, 7, 9, and 11. Use the index() method to find the index of the first occurrence of an element 7 in the tuple.

    
    # Use the tuple index() method
    tuples = (1, 3, 5, 7, 9, 11)
    indx = tuples.index(7)
    print('Index of 7 is', indx)
    
    # Output:
    # Index of 7 is 3
    

    <a id="h-4-get-an-index-from-multiple-occurrences"></a>

    4. Get an Index from Multiple Occurrences

    Alternatively, If you have multiple occurrences of the same element in a tuple and you want to find the index of a specific occurrence, you can use the slice notation to limit the search to a subset of the tuple.

    
    # Get an index from multiple occurrences
    tuples = ('Spark', 'Hadoop', 'Java', 'Hadoop')
    indx = tuples.index('Hadoop', 2)
    print(indx)
    
    # Output
    # 3
    

    You can also use the slice notation to limit the search to a subset of the tuple before a certain index. For example, you can use the index() method to find the index of the first occurrence of the element 'Hadoop' in the subset of the elements from index 0 to index 3 (i.e., the first three elements of the tuple).

    The method returns the index 1, which is the index of the first occurrence of 'Hadoop' in the subset of the tuple.

    
    # Use tuple index() function to slice notation
    tuples = ('Spark', 'Hadoop', 'Java', 'Hadoop', 'Pyspark')
    indx = tuples.index('Hadoop', 0, 3)
    print(indx)
    
    # Output
    # 1
    

    <a id="h-5-raise-valueerror"></a>

    5. Raise ValueError

    You can use the index() method in Python returns the index of the first occurrence of the specified element in a tuple. However, if the specified element is not present in the tuple, it will raise a ValueError exception. For example, since the element 'Pandas' is not present in the tuple tuples object, the index() method will raise a ValueError exception.

    
    # Raise ValueError
    tuples = ('Pyspark', 'Hadoop', 'Java', 'Spark')
    indx = tuples.index('Pandas')
    print(indx)
    
    # Output:
    # Traceback (most recent call last):
    #   File "./prog.py", line 6, in <module>
    # ValueError: tuple.index(x): x not in tuple
    

    <a id="h-6-use-tuple-index-method-with-start-and-end-parameter"></a>

    6. Use Tuple index() Method with Start and End Parameter

    Similarly, you can use the index() method with a start and end position for the search. This is useful when you only want to search for an element within a specific section of the tuple.

    
    # Use tuple index() method with start and end parameter
    tuples = ('Pyspark', 'Hadoop', 'Pandas', 'Java', 'Spark', 'Java')
    indx = tuples.index('Java', 2, 5)
    print('Index of Java in tuples from index/ 2 to 5:', indx)
    
    # Output
    # Index of Java in tuples from index/ 2 to 5: 3
    
    # Find the index of Java starting from index
    indx = tuples.index('Java', 1)
    print('Find the index of Java starting from index/ 1:', indx)
    
    # Output
    # Find the index of Java starting from index/ 1: 3
    
    # Get the index of 'hyperion' between index 2 and 5
    indx = tuples.index('Hyperion', 2, 5)
    print(indx)
    
    # Output
    # ValueError: tuple.index(x): x not in tuple
    

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

    Conclusion

    In this article, I have explained the python tuple index() method, its syntax, and its usage. The tuple index() function returns the index of the first occurrence of a specified element with examples. For more methods refer to Python Tuple Methods

    Happy Learning !!

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

    Related Articles