Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to Iterate over Tuples in Dictionary using Python
In this article, we will learn how to iterate over tuples stored as values in a Python dictionary. Dictionaries can contain tuples as values, and we often need to access and process these tuple elements efficiently.
Methods Used
The following are the various methods used to accomplish this task ?
Using Direct Indexing
Using dictionary.values() Method
Using dictionary.items() Method
Tuples are immutable, ordered collections in Python. Unlike lists, tuples have a fixed length and cannot be modified after creation, making them ideal for storing related data that shouldn't change.
Method 1: Using Direct Indexing
Access specific tuples by their dictionary keys and iterate through elements using indexing or the in operator.
Example
The following program iterates over tuples in a dictionary using direct key access ?
# input dictionary with tuples as values
inputDict = {10: ("Hello", "Tutorialspoint", "Python"),
20: ("dhoni", "virat", "pandya", "rohit sharma"),
30: ("this", "is", "a", "dictionary")}
print("The Tuple mapped with key 10:")
print(inputDict[10])
print()
print("Elements of tuple with key 20:")
# iterating through tuple elements using 'in' operator
for element in inputDict[20]:
print(element)
print()
print("Elements of tuple with key 30:")
# iterating using range() and indexing
for i in range(len(inputDict[30])):
print(inputDict[30][i])
The Tuple mapped with key 10:
('Hello', 'Tutorialspoint', 'Python')
Elements of tuple with key 20:
dhoni
virat
pandya
rohit sharma
Elements of tuple with key 30:
this
is
a
dictionary
Method 2: Using values() Function
The values() method returns all values (tuples) in the dictionary, allowing you to iterate through each tuple without knowing the keys.
Example
The following program iterates over all tuples using values() ?
# input dictionary containing tuples as values
inputDict = {10: ("Hello", "Tutorialspoint", "Python"),
20: ("dhoni", "virat", "pandya", "rohit sharma"),
30: ("this", "is", "a", "dictionary")}
# iterate through all tuples in the dictionary
for tuple_value in inputDict.values():
# iterate through each element in the current tuple
for element in tuple_value:
print(element)
print() # blank line between tuples
Hello Tutorialspoint Python dhoni virat pandya rohit sharma this is a dictionary
Method 3: Using items() Method
The items() method returns key-value pairs, giving you access to both the dictionary key and its corresponding tuple value.
Example
The following program iterates over tuples while also accessing their keys ?
# input dictionary containing tuples as values
inputDict = {10: ("Hello", "Tutorialspoint", "Python"),
20: ("dhoni", "virat", "pandya", "rohit sharma"),
30: ("this", "is", "a", "dictionary")}
# iterate through keys and values (tuples) using items()
for key, tuple_value in inputDict.items():
print(f"Key {key} contains:")
# iterate through elements of each tuple
for element in tuple_value:
print(f" {element}")
print()
Key 10 contains: Hello Tutorialspoint Python Key 20 contains: dhoni virat pandya rohit sharma Key 30 contains: this is a dictionary
Comparison
| Method | Access to Keys? | Best For |
|---|---|---|
| Direct Indexing | Yes (specific keys) | Accessing specific tuples |
values() |
No | Processing all tuples only |
items() |
Yes (all keys) | Processing tuples with their keys |
Conclusion
Use values() when you only need tuple contents, items() when you need both keys and tuples, and direct indexing for specific tuple access. Each method offers different levels of control over dictionary iteration.
