Python program to print the keys and values of the tuple


In Python, tuples are a useful data type for storing a collection of items. Sometimes, it may be necessary to print the keys and values of a tuple in order to understand or debug your code. In this article, we will be discussing, how to print the keys and values of a tuple in Python.

We will go over the syntax for accessing these elements and will provide examples of how to do so. First, we will learn about what is tuple and what we mean by the keys and values of the tuple.

What does a Python tuple mean?

  • Tuples let you store several items in a single variable.

  • The tuple is one of the four built-in data types in Python for storing data collections.

  • The final three are list, set, and dictionary; each has a unique set of characteristics and applications.

  • Tuples are ordered collections that cannot be changed. Tuples are written with round brackets.

  • Tuples are immutable, meaning that once we build a tuple, we are unable to alter, include, or delete any of its elements.

What are the keys and values in a tuple?

Naturally, keys and values do not exist as a pair in a tuple as a tuple only stores single instances of any object.

But if we must make a tuple with keys and values, we can do so by creating a tuple nested with tuples, where each tuple will have two values the first representing its key and the second representing its value.

Example

ListOfTuples = ((1, 4), (3, 5), (4, 5), (5, 6))

In the above code line, there is pair of tuples inside a tuple, so for example the first value of the outer tuple is a tuple with two values. Here the first value which is 1 is the key for that tuple and the second value which is 4 is its data value.

So, as we know that we can access a tuple by indexing, we will be using that indexing to access the inner tuple elements and use it again to access the keys and values.

print(ListOfTuples[0][0])

This will print the key of the first tuple present in the outer tuple. But what if we do not know how many elements are there in the outer tuple? If we use indexing in that case to access the inner elements, we may end up getting an index out-of-range error which signifies that we are trying to access an element that does not exist in the tuple.

Using the len() Python method

To solve this problem, we can use the len method to calculate the number of elements in the outer tuple and use it to print the keys and values up to that number of elements only.

But this will require the use of another statement found in python which is the iterative statement. We have many options to choose from but for the sake of simplicity, we will be using the for a loop.

The for-loop provides an easy way to go over all the elements of an iterable by using the “in” operator.

A = (1, 2, 3)
for item in A:
   print(item)

In the above piece of code, the program will go through each element present in the tuple named A and print the respective element at each iteration. Now let’s move on to the algorithm that will discuss the things we learned above and how to use them to tackle our problem.

Algorithm

  • First, we will create an outer tuple to store the nested inner tuples

  • Each element of the outer tuple will be a tuple object with two elements

  • To print, we will use the for loop that will iterate over the outer tuple

  • At each iteration print the first value of the inner tuple as key

  • Print the second value of the inner tuple as its data value.

At this point, the program will terminate after printing all the keys and values present in the outer tuple.

Example

Program to print the keys and values of a tuple −

ListOfTuples = ((1, 4), (3, 5), (4, 5), (5, 6))
for item in ListOfTuples:
   print(f"Key : {item[0]} Value : {item[1]}")

Output

Key : 1 Value : 4
Key : 3 Value : 5
Key : 4 Value : 5
Key : 5 Value : 6

Conclusion

In this article, we saw what keys and values mean in a tuple in python. And how we can make use of the for loop to print all the keys and values in a tuple of tuples.

Updated on: 17-Feb-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements