10 write a python program to find the index of an item of a tuple

Last update on August 19 2022 21:51:39 (UTC/GMT +8 hours)

Python tuple: Exercise-14 with Solution

Write a Python program to find the index of an item of a tuple.

Sample Solution:-

Python Code:

#create a tuple
tuplex = tuple("index tuple")
print(tuplex)
#get index of the first item whose value is passed as parameter
index = tuplex.index("p")
print(index)
#define the index from which you want to search
index = tuplex.index("p", 5)
print(index)
#define the segment of the tuple to be searched
index = tuplex.index("e", 3, 6)
print(index)
#if item not exists in the tuple return ValueError Exception
index = tuplex.index("y")

Sample Output:

('i', 'n', 'd', 'e', 'x', ' ', 't', 'u', 'p', 'l', 'e')                                                       
8                                                                                                             
8                                                                                                             
3                                                                                                             
Traceback (most recent call last):                                                                            
  File "d0e5ee40-30ab-11e7-a6a0-0b37d4d0b2c6.py", line 14, in <module>                                       
    index = tuplex.index("y")                                                                                 
ValueError: tuple.index(x): x not in tuple   

Pictorial Presentation:

10 write a python program to find the index of an item of a tuple

10 write a python program to find the index of an item of a tuple

10 write a python program to find the index of an item of a tuple

Flowchart:

10 write a python program to find the index of an item of a tuple

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to slice a tuple.
Next: Write a Python program to find the length of a tuple.

Python: Tips of the Day

Cumulative summing a sequence of numbers (calculating the cumulative sum of zero to ten with skips):

>>> import numpy as np
>>> res = list(np.cumsum(range(0,10,2)))
>>> res
[ 0,  2,  6, 12, 20]

The index() method returns the index of the specified element in the tuple.

Example

# tuple containing vowels
vowels = ('a', 'e', 'i', 'o', 'u')

# index of 'e' in vowels index = vowels.index('e')

print(index) # Output: 1


index() Syntax

The syntax of the index() method is:

tuple.index(element, start_index, end_index)

Here, the index() scans the element in the tuple from start_index to end_index.


index() Parameter

The index() method can take one to three parameters:

  • element - the item to scan
  • start_index (optional) - start scanning the element from the start_index
  • end_index (optional) - stop scanning the element at the end_index

index() Return Value

The index() method returns:

  • the index of the given element in the tuple
  • ValueError exception if the element is not found in the tuple

Note: The index() method only returns the first occurrence of the matching element.


Example 1: Python Tuple index()

# tuple containing vowels
vowels = ('a', 'e', 'i', 'o', 'i', 'u')

# index of 'e' in vowels index = vowels.index('e')

print('Index of e:', index)

# index of the first 'i' is returned index = vowels.index('i')

print('Index of i:', index)

Output

Index of e: 1
Index of i: 2

In the above example, we have used the index() method to find the index of a specified element in the vowels tuple.

The element 'e' appears in index 1 in the vowels tuple. Hence, the method returns 1.

The element 'i' appears twice in the vowels tuple. In this case, the index of the first 'i' (which is 2) is returned.


Example 2: index() throws an error if the specified element is absent in the Tuple

# tuple containing numbers
numbers = (0, 2, 4, 6, 8, 10)

# throws error since 3 is absent in the tuple index = numbers.index(3)

print('Index of 3:', index)

Output

ValueError: tuple.index(x): x not in tuple

In the above example, we have used the index() method to find the index of an element that is not present in the numbers tuple.

Here, numbers doesn't contain the number 3. Hence, it throws an exception


Example 3: index() With Start and End Parameters

# alphabets tuple
alphabets = ('a', 'e', 'i', 'o', 'g', 'l', 'i', 'u')

# returns the index of first 'i' in alphabets
index = alphabets.index('i') 

print('Index of i in alphabets:', index)

# scans 'i' from index 4 to 7 and returns its index index = alphabets.index('i', 4, 7)

print('Index of i in alphabets from index 4 to 7:', index)

Output

Index of i in alphabets: 2
Index of i in alphabets from index 4 to 7: 6

In the above example, we have used the index() method to find the index of the element 'i' with the start and end parameters.

Here, 'i' is scanned from index 4 to index 7 in the tuple alphabets. Once found, the index of the scanned 'i' is returned.

How do you find the index of an element in a tuple in Python?

The index() method returns the index of the specified element in the tuple..
element - the item to scan..
start_index (optional) - start scanning the element from the start_index..
end_index (optional) - stop scanning the element at the end_index..

How do you find the index of a list in tuple?

To find the index of a tuple in a list:.
Use a list comprehension to iterate over the list of tuples..
On each iteration, return the tuple item you want to check for..
Use the list. index() method to get the index of the first tuple with the specified value..

Can you get the index of a tuple?

Tuple Indexing We can access elements in a tuple in the same way as we do in lists and strings. Hence, we can access elements simply by indexing and slicing. Furthermore, the indexing is simple as in lists, starting from the index zero.

Can you index through a tuple in Python?

Indexing Tuples As an ordered sequence of elements, each item in a tuple can be called individually, through indexing. Each item corresponds to an index number, which is an integer value, starting with the index number 0 .