Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Python – Iterate over Tuples in Dictionary

  • Last Updated : 05 Nov, 2021

In this article, we will discuss how to Iterate over Tuples in Dictionary in Python. 

Method 1: Using index

We can get the particular tuples by using an index:

Syntax:

dictionary_name[index]

To iterate the entire tuple values in a particular index

for i in range(0, len(dictionary_name[index])):
    print(dictionary_name[index][i]

 Example:

Python




# Ininitalizing a dictionary
myDict = {1: ("Apple", "Boy", "Cat"),
          2: ("Geeks", "For", "Geeks"),
          3: ("I", "Am", "Learning", "Python")}
  
print("Tuple mapped with the key 1 =>"),
  
# Directly printing entire tuple mapped 
# with the key 1
print(myDict[1])
  
print("Tuple mapped with the key 2 =>"),
  
# Printing tuple elements mapped with 
# the key 2 one by one
for each in myDict[2]:
    print(each),
  
print("")
print("Tuple mapped with the key 3 =>"),
  
# Accessing tuple elements mapped with 
# the key 3 using index
for i in range(0, len(myDict[3])):
    print(myDict[3][i]),

Output:

Tuple mapped with the key 1 =>
('Apple', 'Boy', 'Cat')
Tuple mapped with the key 2 =>
Geeks
For
Geeks

Tuple mapped with the key 3 =>
I
Am
Learning
Python

Method 2 : Using values() method

we can use dictionary.values() to iterate over the tuples in a dictionary with for loop

Syntax:

for i in dictionary_name.values():
  for j in i:
      print(j)
  print(" ")

Example:

Python3




# Ininitalizing a dictionary
myDict = {1: ("Apple", "Boy", "Cat"),
          2: ("Geeks", "For", "Geeks"),
          3: ("I", "Am", "Learning", "Python")}
  
# iterate over all tuples using
# values() method
for i in myDict.values():
    for j in i:
        print(j)
    print(" ")

Output:

Apple
Boy
Cat
 
Geeks
For
Geeks
 
I
Am
Learning
Python


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!