How to Iterate over Tuples in Dictionary using Python



In this article, we will learn how to iterate over the tuples in Dictionary in python.

Methods Used

The following are the various methods used to accomplish this task −

  • Using Indexing

  • Using dictionary.values() Method

  • Using dictionary.items() Method

Tuples are an immutable, unordered data type used to store collections in Python. Lists and tuples are similar in many ways, but a list has a variable length and is mutable in comparison to a tuple which has a fixed length and is immutable.

Method 1: Using Indexing

len() function − The number of items in an object is returned by the len() method.The len() function returns the number of characters in a string when the object is a string.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task. −

  • Create a variable to store the input dictionary containing the values as a tuple.

  • Print the whole tuple mapped with the specific key(here 10) of the input dictionary by using the key

  • Traverse through the tuple elements of the dictionary using in operator to print the elements of the tuple mapped with the key of the dictionary one by one.

  • Traverse till the length tuple elements of the dictionary using range() function to print the elements of the tuple mapped with the key of the dictionary.

Example

The following program iterate over the tuples in the dictionary and prints the elements of each tuple using Indexing −

# input dictionary
inputDict = {10: ("Hello", "Tutorialspoint", "Python"),
             20: ("dhoni", "virat", "pandya", "rohit sharma"),
             30: ("this", "is", "a", "dictionary")}
print("The Tuple mapped with the key 10 of the dictionary is:"),

# printing the whole tuple mapped with the key 10 of the dictionary
print(inputDict[10])
print()

print("The Tuple mapped with the key 20 of the dictionary is:"),

#  printing the elements of the tuple mapped with
# the key 20 of the dictionary one-by-one using for loop and in operator
for i in inputDict[20]:
   # printing the corresponding element
      print(i),

print()
print("The Tuple mapped with the key 30 of the dictionary is:"),

# printing the elements of the tuple mapped with
# the key 20 of the dictionary one-by-one using for loop and range()
for k in range(0, len(inputDict[30])):
   # accessing the corresponding element
      print(inputDict[30][k])

Output

On executing, the above program will generate the following output −

The Tuple mapped with the key 10 of the dictionary is:
('Hello', 'Tutorialspoint', 'Python')

The Tuple mapped with the key 20 of the dictionary is:
dhoni
virat
pandya
rohit sharma

The Tuple mapped with the key 30 of the dictionary is:
this
is
a
dictionary

Method 2: Using values() Function

Use the dictionary.values() function to traverse through the tuples in a dictionary with the for loop.

values() function(the dict.values() method provides a view object that displays a list of all the values in the dictionary in order of insertion)

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task. −

  • Create a variable to store the input dictionary containing the values as a tuple.

  • Use the for loop, to traverse through the values(all tuples) of a dictionary using values() function.

  • Use another nested for loop, to traverse within each tuple again.

  • Print the corresponding element of the tuple

Example

The following program iterate over the tuples in the dictionary and prints the elements of each tuple using dictionary.values() function −

# input dictionary containing the values as a tuple
inputDict = {10: ("Hello", "Tutorialspoint", "Python"),
             20: ("dhoni", "virat", "pandya", "rohit sharma"),
             30: ("this", "is", "a", "dictionary")}

# traversing through the values(tuples) of a dictionary using values() function
for p in inputDict.values():
   # traversing within each tuple again using another nested for loop
      for q in p:
         # printing the elements of the corresponding tuple one by one
         print(q)
      print(" ")

Output

On executing, the above program will generate the following output −

Hello
Tutorialspoint
Python
 
dhoni
virat
pandya
rohit sharma
 
this
is
a
dictionary

Method 3: Using dictionary.items() Method

Use the dictionary.items() function to traverse through the tuples in a dictionary with the for loop.

The items() function returns a view object i.e, it contains the key-value pairs of the dictionary, as tuples in a list.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task. −

  • Create a variable to store the input dictionary containing the values as a tuple.

  • Use the for loop, to traverse through the keys, and values(all tuples) of a dictionary using the items() function.

  • Traverse through all the elements of the tuple using another nested for loop.

  • Print the tuple elements.

Example

The following program iterate over the tuples in the dictionary and prints the elements of each tuple using dictionary.items() function −

# input dictionary containing the values as a tuple
inputDict = {10: ("Hello", "Tutorialspoint", "Python"),
		20: ("dhoni", "virat", "pandya", "rohit sharma"),
		30: ("this", "is", "a", "dictionary")}
 

# traversing through keys,values(tuples) of a dictionary using items() function
for key, tuplevalues in inputDict.items():
   # traversing within values of each tuple again using another nested for loop
	   for value in tuplevalues:
   # printing the elements of the corresponding tuple elements one by one
		print(value)
	print(" ")

Output

On executing, the above program will generate the following output −

Hello
Tutorialspoint
Python
 
dhoni
virat
pandya
rohit sharma
 
this
is
a
dictionary

Conclusion

This article provided us with three different methods for iterating through tuples in the given dictionary. We learned how to use the values() function to get the dictionary's values. Additionally, we learned how to use the items() method to traverse through the dictionary's keys and values.


Advertisements