The Wayback Machine - https://web.archive.org/web/20241116155516/https://www.geeksforgeeks.org/python-tuple-index-method/
Open In App

Python Tuple – index() Method

Last Updated : 31 May, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

While working with tuples many times we need to access elements at a certain index but for that, we need to know where exactly is that element, and here comes the use of the index() function. In this article, we will learn about the index() function used for tuples in Python.

Example :

The Index() method returns the first occurrence of the given element from the tuple.

Python3




my_tuple = ( 4, 2, 5, 6, 7, 5)
print(my_tuple.index(5))


Output :

2

Python Tuple Index () Syntax

Syntax: tuple.index(element, start, end)

Parameters:

  • element: The element to be searched.
  • start (Optional): The starting index from where the searching is started
  • end (Optional): The ending index till where the searching is done

Return type: Integer value denoting the index of the element.

Tuple Index() in Python Examples

Find the index of the element

Here we are finding the index of a particular element in a tuple.

Python3




# Creating tuples
Tuple = ( 1, 3, 4, 2, 5, 6 )
   
# getting the index of 3
res = Tuple.index(3)
print('Index of 3 is', res)


Output :

Value of Index of 3 is 1

Get an index from multiple occurrences

Here we are finding the index of a particular element in a tuple with multiple occurrences but here it only returns the first occurrence of that element.

Python3




# Creating tuples
Tuple = ( 3, 3, 5, 7, 3, 3 )
   
# getting the index of 3
res = Tuple.index(3)
print('Index of 3 is', res)


Output:

Index of 3 is 0

Working on the index() With Start and End Parameters

Here we are finding the index of a particular element in a tuple in the given range.

Python3




# alphabets tuple
alphabets = ('G', 'e', 'e', 'k', 's', 'f', 'o',
             'r', 'G', 'e', 'e', 'k', 's')
 
# scans 'G' from index 4 to 10 and
# returns its index
index = alphabets.index('G', 4, 10)
 
print('Index of G in alphabets from index\
                            4 to 10:', index)


Output:

Index of G in alphabets from index 4 to 10: 8

Index of the Element did not Present in the Tuple

Here we are finding the index of a particular element in a tuple that does not exist.

Python3




t = ('G', 'F', 'G')
 
# accessing element not
# present in the tuple
print(t.index('i'))


Output:

Traceback (most recent call last):

  File "08f51264-102d-4f65-b2d3-692a5a750419.py", line 4, in <module>

    print(t.index('i'))

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


Previous Article
Next Article

Similar Reads

Python | Sort tuple list by Nth element of tuple
Sometimes, while working with Python list, we can come across a problem in which we need to sort the list according to any tuple element. These must be a generic way to perform the sort by particular tuple index. This has a good utility in web development domain. Let's discuss certain ways in which this task can be performed. Method #1: Using sort(
8 min read
Python | Replace tuple according to Nth tuple element
Sometimes, while working with data, we might have a problem in which we need to replace the entry in which a particular entry of data is matching. This can be a matching phone no, id etc. This has it's application in web development domain. Let's discuss certain ways in which this task can be performed. Method #1: Using loop + enumerate() This task
8 min read
Python - Raise elements of tuple as power to another tuple
Sometimes, while working with records, we can have a problem in which we may need to perform exponentiation, i.e power of tuples. This problem can occur in day-day programming. Let’s discuss certain ways in which this task can be performed. Method #1: Using zip() + generator expression The combination of above functions can be used to perform this
8 min read
Python - Convert Tuple String to Integer Tuple
Interconversion of data is a popular problem developer generally deal with. One can face a problem to convert tuple string to integer tuple. Let's discuss certain ways in which this task can be performed. Method #1 : Using tuple() + int() + replace() + split() The combination of above methods can be used to perform this task. In this, we perform th
7 min read
Python - Convert Tuple to Tuple Pair
Sometimes, while working with Python Tuple records, we can have a problem in which we need to convert Single tuple with 3 elements to pair of dual tuple. This is quite a peculiar problem but can have problems in day-day programming and competitive programming. Let's discuss certain ways in which this task can be performed. Input : test_tuple = ('A'
10 min read
Python - Flatten tuple of List to tuple
Sometimes, while working with Python Tuples, we can have a problem in which we need to perform the flattening of tuples, which have listed as their constituent elements. This kind of problem is common in data domains such as Machine Learning. Let's discuss certain ways in which this task can be performed. Input : test_tuple = ([5], [6], [3], [8]) O
7 min read
Python Program to Convert Tuple Matrix to Tuple List
Given a Tuple Matrix, flatten to tuple list with each tuple representing each column. Example: Input : test_list = [[(4, 5), (7, 8)], [(10, 13), (18, 17)]] Output : [(4, 7, 10, 18), (5, 8, 13, 17)] Explanation : All column number elements contained together. Input : test_list = [[(4, 5)], [(10, 13)]] Output : [(4, 10), (5, 13)] Explanation : All co
8 min read
Python program to convert Set into Tuple and Tuple into Set
Let's see how to convert the set into tuple and tuple into the set. For performing the task we are use some methods like tuple(), set(), type(). tuple(): tuple method is used to convert into a tuple. This method accepts other type values as an argument and returns a tuple type value.set(): set method is to convert other type values to set this meth
7 min read
Python Program to find tuple indices from other tuple list
Given Tuples list and search list consisting of tuples to search, our task is to write a Python Program to extract indices of matching tuples. Input : test_list = [(4, 5), (7, 6), (1, 0), (3, 4)], search_tup = [(3, 4), (8, 9), (7, 6), (1, 2)]Output : [3, 1]Explanation : (3, 4) from search list is found on 3rd index on test_list, hence included in r
8 min read
Python Program to Merge tuple list by overlapping mid tuple
Given two lists that contain tuples as elements, the task is to write a Python program to accommodate tuples from the second list between consecutive tuples from the first list, after considering ranges present between both the consecutive tuples from the first list. Input : test_list1 = [(4, 8), (19, 22), (28, 30), (31, 50)], test_list2 = [(10, 12
11 min read
Tuple within a Tuple in Python
In the realm of Python programming, tuples offer a flexible solution for organizing data effectively. By allowing the inclusion of other tuples within them, tuples further enhance their capabilities. In this article, we will see how we can use tuple within a tuple in Python. Using Tuple within a Tuple in PythonBelow are some of the ways by which we
4 min read
Python | Get first index values in tuple of strings
Yet another peculiar problem which might not be common, but can occur in python programming while playing with tuples. Since tuples are immutable, they are difficult to manipulate and hence knowledge of possible variation solutions always helps. This articles solves problem of extracting only the first index element of each string in tuple. Let's d
5 min read
Python | Accumulative index summation in tuple list
Sometimes, while working with data, we can have a problem in which we need to find accumulative summation of each index in tuples. This problem can have applications in web development and competitive programming domain. Let's discuss certain way in which this problem can be solved. Method 1: Using accumulate() + sum() + lambda + map() + tuple() +
8 min read
Python | Minimum K records of Nth index in tuple list
Sometimes, while working with data, we can have a problem in which we need to get the minimum of elements filtered by the Nth element of record. This has a very important utility in web development domain. Let’s discuss certain ways in which this task can be performed. Method #1 : Using filter() + lambda + set() + list comprehension The combination
9 min read
Python | Nth tuple index Subtraction by K
Many times, while working with records, we can have a problem in which we need to change the value of tuple elements. This is a common problem while working with tuples. Let’s discuss certain ways in which K can be subtracted to Nth element of tuple in list. Method #1 : Using loop Using loops this task can be performed. In this, we just iterate the
6 min read
Python | Divide constant to Kth Tuple index
Many times, while working with records, we can have a problem in which we need to change the value of tuple elements. This is a common problem while working with tuples. Let’s discuss certain ways in which N can be divided by Kth element of tuple in list. Method #1: Using loop Using loops this task can be performed. In this, we just iterate the lis
4 min read
Python - Similar index pairs in Tuple lists
Sometimes, while working with Python tuples, we can have a problem in which we need to perform similar index pairing. This kind of problem is peculiar, but can occur across certain domains. Let's discuss certain way in which this task can be performed. Input : test_list1 = [(5, ), (1, ), (8, ), (10, )] test_list2 = [(8, ), (1, ), (11, ), (9, )] Out
5 min read
Python - Kth Index Tuple List Mean
Sometimes, while working with Python tuple, we can have a problem in which we need to compute average of any particular index of tuples in a list. This kind of problem can have application in data domain such as web development. Let's discuss certain ways in which this task can be performed. Input : test_list = [('Gfg', 1), ('is', 5), ('best', 7)],
6 min read
Python - Remove Kth Index Duplicates in Tuple
Sometimes, while working with Python records, we can have a problem in which we need to remove all the tuples, which have similar Kth index elements in list of records. This kind of problem is common in day-day and web development domain. Let's discuss certain ways in which this task can be performed. Input : test_list = [(4, 5, 6), (2, 3, 4), (1,
7 min read
Python - Closest Pair to Kth index element in Tuple
In the given problem, condition K specifies the maximum allowable difference between corresponding elements of tuples, i.e., it represents the proximity threshold for finding the nearest tuple. The goal is to find the tuple in the list whose elements have the smallest maximum difference compared to the given tuple tup, and this difference should no
7 min read
Python List index() - Find Index of Item
List index() method searches for a given element from the start of the list and returns the position of the first occurrence. Example: [GFGTABS] Python # list of animals Animals= ["cat", "dog", "tiger"] # searching positiion of dog print(Animals.index("dog")) [/GFGTABS]Output1 List index() Method Syntaxlist_n
5 min read
Python Tuple - min() Method
While working with tuples many times we need to find the minimum element in the tuple, and for this, we can also use min(). In this article, we will learn about the min() method used for tuples in Python. Syntax of Tuple min() MethodSyntax: min(object) Parameters: object: Any iterable like Tuple, List, etc. Return type: minimum element from the tup
2 min read
Python Tuple - max() Method
While working with tuples many times we need to find the maximum element in the tuple, and for this, we can also use max(). In this article, we will learn about the max() method used for tuples in Python. Example Tuple =( 4, 2, 5, 6, 7, 5) Input: max(Tuple) Output: 7 Explanation: The max() method returns the largest element of the given tuple. Pyth
2 min read
Python Tuple - len() Method
While working with tuples many times we need to find the length of the tuple, and, instead of counting the number of elements with loops, we can also use Python len(). We will learn about the len() method used for tuples in Python. Example: C/C++ Code Tuple =( 1, 0, 4, 2, 5, 6, 7, 5) print(len(Tuple)) Output : 8Python tuple len() Method SyntaxSynta
2 min read
Python Tuple count() Method
In this article, we will learn about the count() method used for tuples in Python. The count() method of a Tuple returns the number of times the given element appears in the tuple. Example C/C++ Code tuple = (1, 2, 3, 1, 2, 3, 1, 2, 3) print(tuple.count(3)) Output : 3Python Tuple count() Method Syntax Syntax: tuple.count( ele ) Parameters: ele: Any
3 min read
Numpy: Index 3D array with index of last axis stored in 2D array
NumPy, or Numerical Python, is a powerful library for efficient numerical computation in Python. One of the key features of NumPy is its ability to perform advanced indexing, which allows to access and manipulate specific elements of an array based on complex conditions. In this article, we will explore how to index a 3D array using a 2D array of i
5 min read
Python program to Sort a List of Tuples in Increasing Order by the Last Element in Each Tuple
The task is to write a Python Program to sort a list of tuples in increasing order by the last element in each tuple. Input: [(1, 3), (3, 2), (2, 1)] Output: [(2, 1), (3, 2), (1, 3)] Explanation: sort tuple based on the last digit of each tuple. Methods #1: Using sorted(). Sorted() method sorts a list and always returns a list with the elements in
7 min read
Python | Count the elements in a list until an element is a Tuple
In this problem, we need to accept a list. The list can have nested tuples. We need to count the elements in a list until a tuple has been encountered. Examples: Input : [4, 5, 6, 10, (1, 2, 3), 11, 2, 4] Output : 4 Input : [4, (5, 6), 10, (1, 2, 3), 11, 2, 4] Output : 1 Method #1: In this program we will use the concept of isinstance() to verify w
2 min read
Python | Count occurrences of an element in a Tuple
In this program, we need to accept a tuple and then find the number of times an item is present in the tuple. This can be done in various ways, but in this article, we will see how this can be done using a simple approach and how inbuilt functions can be used to solve this problem. Examples: Tuple: (10, 8, 5, 2, 10, 15, 10, 8, 5, 8, 8, 2)Input : 4
3 min read
Python | Sort a tuple by its float element
In this article, we will see how we can sort a tuple (consisting of float elements) using its float elements. Here we will see how to do this by using the built-in method sorted() and how can this be done using in place method of sorting. Examples: Input : tuple = [('lucky', '18.265'), ('nikhil', '14.107'), ('akash', '24.541'), ('anand', '4.256'),
3 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg