In the previous article, we have discussed Python Tuple count() method with Examples
Tuples in Python:
Tuples are a data structure in Python that stores an ordered succession of values. They are unchangeable. This signifies that the values of a tuple cannot be changed. They allow you to save an organized list of items. A tuple, for example, can be used to hold a list of employee names.
Tuple index() Method in Python:
The index() method returns the position in the given tuple of the given element.
Syntax:
gvntuple .index(element, start, end)
Parameter Values:Â
The tuple index() method can accept up to three arguments:
element – the to-be-searched element
start (optional) – Begin searching from this index
end (optional) – Search the element all the way up to this index
Return Value:
The index() method returns the index of the specified tuple element.
A ValueError exception is thrown if the element is not found.
Note:
Only the first occurrence of the matching element is returned by the index() method.
Examples:
Example1:
Input:
Given Tuple= ('hello', 'this', 'is', 'this', 'python', 'programs') Given Element = 'this'
Output:
The given element { this } in the given tuple ('hello', 'this', 'is', 'this', 'python', 'programs') is present at the index : 1
Example2:
Input:
Given Tuple= ('hello', 'this', 'is', 'this', 'python', 'programs') Given Element = 'morning'
Output:
Traceback (most recent call last): File "/home/a3ee758dfee37d1702ad5c70e38293aa.py", line 8, in <module> resltind = gvntupl.index(gvnele) ValueError: tuple.index(x): x not in tuple
Tuple index() Method with Examples in Python
Method #1: Using Built-in Functions (Static Input)
Example-1: If the element is present in the given Tuple
Approach:
- Give the Tuple as the static input and store it in a variable.
- Give the element whose index must be located in the given Tuple and store it in another variable.
- Pass the given element to the index function for the given Tuple and store the result in resltind variable.
- Print the resltind value.
- The Exit of the Program.
Below is the implementation:
# Give the tuple as the static input and store it in a variable. gvntupl = ('hello', 'this', 'is', 'this', 'python', 'programs') # Give the element whose index must be located in the given tuple # and store it in another variable. gvnele = 'this' # Pass the given element to the index function for the given tuple # and store the result in resltind variable. resltind = gvntupl.index(gvnele) # Print the resltind value. print('The given element {', gvnele, '} in the given tuple', gvntupl, 'is present at the index :') print(resltind)
Output:
The given element { this } in the given tuple ('hello', 'this', 'is', 'this', 'python', 'programs') is present at the index : 1
Example-2: If the element is not present in the given Tuple
Below is the implementation:
# Give the tuple as the static input and store it in a variable. gvntupl = ('hello', 'this', 'is', 'this', 'python', 'programs') # Give the element whose index must be located in the given tuple # and store it in another variable. gvnele = 'now' # Pass the given element to the index function for the given tuple # and store the result in resltind variable. resltind = gvntupl.index(gvnele) # Print the resltind value. print('The given element {', gvnele, '} in the given tuple', gvntupl, 'is present at the index :') print(resltind)
Output:
Traceback (most recent call last): File "/home/a3ee758dfee37d1702ad5c70e38293aa.py", line 8, in <module> resltind = gvntupl.index(gvnele) ValueError: tuple.index(x): x not in tuple
Example-3: Giving Start Index
Below is the implementation:
# Give the tuple as the static input and store it in a variable. gvntupl = ('hello', 'this', 'is', 'this', 'python', 'programs') # Give the element whose index must be located in the given tuple # and store it in another variable. gvnele = 'this' # Pass the given element to the index function for the given tuple # and store the result in resltind variable. # searching the element after 1 st index by passing second argument as 1 resltind = gvntupl.index(gvnele, 1) # Print the resltind value. print('The given element {', gvnele, '} in the given tuple', gvntupl, 'is present at the index :') print(resltind)
Output:
The given element { this } in the given tuple ('hello', 'this', 'is', 'this', 'python', 'programs') is present at the index : 1
Example-4: Giving Start and End Index
Below is the implementation:
# Give the tuple as the static input and store it in a variable. gvntupl = ('hello', 'this', 'is', 'this', 'python', 'programs') # Give the element whose index must be located in the given tuple # and store it in another variable. gvnele = 'hello' # Pass the given element to the index function for the given tuple # and store the result in resltind variable. # searching the element after 1 st index to 3 index by passing # second argument as 1 and third argument as 3 resltind = gvntupl.index(gvnele, 1, 3) # Print the resltind value. print('The given element {', gvnele, '} in the given tuple', gvntupl, 'is present at the index :') print(resltind)
Output:
Traceback (most recent call last): File "/home/0eabc1fa82ac939d529fa63ad0fd9be6.py", line 10, in <module> resltind = gvntupl.index(gvnele, 1, 3) ValueError: tuple.index(x): x not in tuple
Explanation:
The element hello is present at the 0th index but not between the indices, resulting in an error.
Method #2: Using Built-in Functions (User Input)
Approach:
- Give the Tuple as user input using tuple(),map(),input(),and split() functions.
- Store it in a variable.
- Give the element whose index must be located in the given Tuple as user input using the input() function and store it in another variable.
- Pass the given element to the index function for the given Tuple and store the result in resltind variable.
- Print the resltind value.
- The Exit of the Program.
Below is the implementation:
# Give the Integer tuple as user input using tuple (),map(),input(),and split() functions. # Store it in a variable. gvntupl = tuple(map(int, input('Enter some random tuple elements = ').split())) # Give the element as user input using the int(), input() functions, # and store it in another variable. gvnele = int(input('Enter some random element = ')) # Pass the given element to the index function for the given tuple # and store the result in resltind variable. resltind = gvntupl.index(gvnele) # Print the resltind value. print('The given element {', gvnele, '} in the given tuple', gvntupl, 'is present at the index :') print(resltind)
Output:
Enter some random tuple elements = 4 1 7 8 3 11 7 3 Enter some random element = 3 The given element { 3 } in the given tuple (4, 1, 7, 8, 3, 11, 7, 3) is present at the index : 4
Find a Comprehensive Collection of Python Built in Functions that you need to be aware of and use them as a part of your program.