Iterate over a tuple in Python Last Updated : 11 Oct, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Python provides several ways to iterate over tuples. The simplest and the most common way to iterate over a tuple is to use a for loop. Below is an example on how to iterate over a tuple using a for loop. Python t = ('red', 'green', 'blue', 'yellow') # iterates over each element of the tuple 't' # and prints it for color in t: print(color) Outputred green blue yellow Let's discuss other different methods to iterate through tuples.Table of ContentUsing range() with len()Using while LoopUsing enumerate() FunctionUsing range() with len()Another way to iterate over a tuple is to use the range() along with len(). This method is useful when we need access to the index of each element. Python t = ('red', 'green', 'blue', 'yellow') # Calculate the lenth of a tuple n = len(t) # Iterates through the indices from 0 to 3 for i in range(n): print(f"Index {i}: {t[i]}") OutputIndex 0: red Index 1: green Index 2: blue Index 3: yellow Using while LoopWe can also use a while loop to iterate over a tuple. This method is useful when we want more control over the iteration steps. Python t = ('red', 'green', 'blue', 'yellow') # Start from the first index i = 0 # Loops till the last index (i.e., 3) while i < len(t): print(t[i]) i += 1 Outputred green blue yellow Using enumerate()We can use the enumerate() function of iterate over the tuple, if we need the index along with the value. This function returns both the index and the corresponding value of the tuple. Python t = ('red', 'green', 'blue', 'yellow') # Here, i and color stores the index and value respectively for i, color in enumerate(t): print(f"Index {i}: {color}") OutputIndex 0: red Index 1: green Index 2: blue Index 3: yellow Related Articles:Iterate through list of tuples in PythonIterate over Tuples in DictionaryWays to iterate tuple list of lists Comment More infoAdvertise with us Next Article Tuples in Python P puja2370 Follow Improve Article Tags : Python Geeks Premier League Geeks Premier League 2023 Practice Tags : python Similar Reads Create a List of Tuples in Python The task of creating a list of tuples in Python involves combining or transforming multiple data elements into a sequence of tuples within a list. Tuples are immutable, making them useful when storing fixed pairs or groups of values, while lists offer flexibility for dynamic collections. For example 3 min read How to iterate through list of tuples in Python In Python, a list of tuples is a common data structure used to store paired or grouped data. Iterating through this type of list involves accessing each tuple one by one and sometimes the elements within the tuple. Python provides several efficient and versatile ways to do this. Letâs explore these 2 min read Python | Decimal as_tuple() method Decimal#as_tuple() : as_tuple() is a Decimal class method which returns named tuple representation of the Decimal value. Syntax: Decimal.as_tuple() Parameter: Decimal values Return: tuple with values - sign - digits - exponent Code #1 : Example for as_tuple() method Python3 # Python Program explaini 2 min read Tuples in Python Python Tuple is a collection of objects separated by commas. A tuple is similar to a Python list in terms of indexing, nested objects, and repetition but the main difference between both is Python tuple is immutable, unlike the Python list which is mutable.Python# Note : In case of list, we use squa 7 min read Python Tuple Methods Python Tuples is an immutable collection of that are more like lists. Python Provides a couple of methods to work with tuples. In this article, we will discuss these two methods in detail with the help of some examples. Count() MethodThe count() method of Tuple returns the number of times the given 3 min read Convert Tuple to List in Python In Python, tuples and lists are commonly used data structures, but they have different properties:Tuples are immutable: their elements cannot be changed after creation.Lists are mutable: they support adding, removing, or changing elements.Sometimes, you may need to convert a tuple to a list for furt 2 min read Like