How to iterate through list of tuples in Python Last Updated : 17 Dec, 2024 Comments Improve Suggest changes Like Article Like Report 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 methodsUsing a for LoopA basic for loop allows us to iterate through the list and access each tuple directly. It is Ideal for straightforward traversal when the entire tuple is needed. Python x = [(1, 'a'), (2, 'b'), (3, 'c')] # Iterate through the list and print each tuple for tup in x: print(tup) Output(1, 'a') (2, 'b') (3, 'c') Let's explore some other methods on how to iterate through a list of tuples in PythonTable of ContentUsing enumerate()Using List ComprehensionUsing a While Loop with IndexingUsing enumerate()enumerate()function adds an index to each tuple making it easier to track their positions in the list. The enumerate() function returns both the index and the tuple in each iteration. It is Ideal when the index of each tuple is required along with its value. Python x = [(1, 'a'), (2, 'b'), (3, 'c')] # Iterate through the list with index and tuple for idx, tup in enumerate(x): print(f"Index: {idx}, Tuple: {tup}") OutputIndex: 0, Tuple: (1, 'a') Index: 1, Tuple: (2, 'b') Index: 2, Tuple: (3, 'c') Using List ComprehensionFor compact code, list comprehension can be used to iterate and perform operations on each tuple. It is best for creating new lists based on operations on tuples. Python x = [(1, 'a'), (2, 'b'), (3, 'c')] # unpack the tuple into num and char #format the result as "num-char" result = [f"{num}-{char}" for num, char in x] print(result) Output['1-a', '2-b', '3-c'] Using a while Loop with IndexingA while loop can iterate through a list of tuples by tracking the index. The while loop provides complete control over the iteration process. It is useful when we need to modify the index dynamically during iteration. Python x = [(1, 'a'), (2, 'b'), (3, 'c')] i = 0 while i < len(x): print(x[i]) i += 1 Output(1, 'a') (2, 'b') (3, 'c') Comment More infoAdvertise with us Next Article How to iterate through list of tuples in Python S sravankumar_171fa07058 Follow Improve Article Tags : Python python-list Python List-of-Tuples Practice Tags : pythonpython-list Similar Reads Python Tuples A tuple in Python is an immutable ordered collection of elements. Tuples are similar to lists, but unlike lists, they cannot be changed after their creation (i.e., they are immutable). Tuples can hold elements of different data types. The main characteristics of tuples are being ordered , heterogene 6 min read Tuple Operations 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 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 Create a tuple from string and list - Python The task of creating a tuple from a string and a list in Python involves combining elements from both data types into a single tuple. The list elements are added as individual items and the string is treated as a single element within the tuple. For example, given a = ["gfg", "is"] and b = "best", t 3 min read Access front and rear element of Python tuple Sometimes, while working with records, we can have a problem in which we need to access the initial and last data of a particular record. This kind of problem can have application in many domains. Let's discuss some ways in which this problem can be solved. Method #1: Using Access Brackets We can pe 6 min read Python - Element Index in Range Tuples Sometimes, while working with Python data, we can have a problem in which we need to find the element position in continuous equi ranged tuples in list. This problem has applications in many domains including day-day programming and competitive programming. Let's discuss certain ways in which this t 7 min read Unpacking a Tuple in Python Tuple unpacking is a powerful feature in Python that allows you to assign the values of a tuple to multiple variables in a single line. This technique makes your code more readable and efficient. In other words, It is a process where we extract values from a tuple and assign them to variables in a s 2 min read Unpacking Nested Tuples-Python The task of unpacking nested tuples in Python involves iterating through a list of tuples, extracting values from both the outer and inner tuples and restructuring them into a flattened format. For example, a = [(4, (5, 'Gfg')), (7, (8, 6))] becomes [(4, 5, 'Gfg'), (7, 8, 6)].Using list comprehensio 3 min read Python | Slice String from Tuple ranges Sometimes, while working with data, we can have a problem in which we need to perform the removal from strings depending on specified substring ranges. Let's discuss certain ways in which this task can be performed. Method #1: Using loop + list slicing: This is the brute force task to perform this t 3 min read Python - Clearing a tuple Sometimes, while working with Records data, we can have a problem in which we may require to perform clearing of data records. Tuples, being immutable cannot be modified and hence makes this job tough. Let's discuss certain ways in which this task can be performed. Method #1 : Using list() + clear() 4 min read Like