Python: Print items of a dictionary line by line (4 ways)

How to print items of a dictionary line by line in python ?

In python a dictionary is one of the important datatype which is used to store data values in key : value pair. Generally keys and values are mapped one-to-one. But it is not impossible that we can not map multiple values to a single key. So in this article we will discuss how we can map multiple values per key in dictionary.

Syntax of dictionary :

dictionary_name = {key1: value1, key2: value2}

where,

  • key1, key2… represents keys in a dictionary. These keys can be a string, integer, tuple, etc. But keys needs to be unique.
  • value1, value2… represents values in dictionary. These values can be strings, numbers, list or list within a list etc.
  • key and value is separated by : (colon) symbol.
  • key-value pair symbol is separated by , (comma) symbol.

Example of a dictionary population where multiple values are associated with single key.

population = {"Odisha": 40000000, "Telangana": 50000000, "Delhi": 80000000, "Goa": 10000000}

So, let’s first create a dictionary and we will see how it prints the dictionary in a single line.

#Program

#dictionary created
population = {"Odisha": 40000000, "Telangana": 50000000, "Delhi": 80000000, "Goa": 10000000} 
#printing dictionary in a line
print("Printing dictionary in a single line :") 
print(population)
Output :
Printing dictionary in a single line :
population = {"Odisha": 40000000, "Telangana": 50000000, "Delhi": 80000000, "Goa": 10000000}

It was very easy to print dictionary in a single line as to print the dictionary we just passed the dictionary name i.e population in the print statement. As the dictionary is small so we printed it in a single line also we understood it easily.

But think about a situation when the dictionary is too big and we need to print the dictionary line by line means one key-value pair in a single line then next key-value pair in next line and so on. It will be very easy for us also to understand a big dictionary very easily. So, in this article we will discuss how we can print items of a dictionary in line by line.

Method -1 : Print a dictionary line by line using for loop & dict.items()

In python there is a function items( ), we can use that along with for loop to print the items of dictionary line by line. Actually dict.items( ) returns an iterable view object of the dictionary which is used to iterate over key-value pairs in the dictionary.

So, let’s take an example to understand it more clearly.

#Program

#dictionary created
population = {"Odisha": 40000000, "Telangana": 50000000, "Delhi": 80000000, "Goa": 10000000} 
#printing dictionary in line by line
# Iterating over key-value pairs in dictionary and printing them
for key, value in population.items():
    print(key, ' : ', value)
Output :
Odisha: 40000000
Telangana: 50000000
Delhi: 80000000
Goa: 10000000

Method -2 : Print a dictionary line by line by iterating over keys

Like in method-1 we did iterate over key-value pair, in method-2 we can only iterate over key and for each key we can access its value and print the respective value.

So, let’s take an example to understand it more clearly.

#Program

#dictionary created
population = {"Odisha": 40000000, "Telangana": 50000000, "Delhi": 80000000, "Goa": 10000000} 
#printing dictionary in line by line
# Iterating over key in dictionary and printing the value of that key
for key in population:
    print(key, ' : ', population[key])
Output :
Odisha: 40000000
Telangana: 50000000
Delhi: 80000000
Goa: 10000000

Method -3 : Print a dictionary line by line using List Comprehension

Using list comprehension and dict.items(), the contents of a dictionary can be printed line by line.

So, let’s take an example to understand it more clearly.

#Program

#dictionary created
population = {"Odisha": 40000000, "Telangana": 50000000, "Delhi": 80000000, "Goa": 10000000} 
#printing dictionary in line by line
[print(key,':',value) for key, value in population.items()]
Output : 
Odisha: 40000000 
Telangana: 50000000 
Delhi: 80000000 
Goa: 10000000

Method -4 : Print a dictionary line by line using json.dumps()

In python, json.dumps( ) is provided by json module  to serialize the passed object to a json like string. So to print the dictionary line by line we can pass that dictionary in json.dumps( ).

So, let’s take an example to understand it more clearly.

#Program

import json
#dictionary created
population = {"Odisha": 40000000, "Telangana": 50000000, "Delhi": 80000000, "Goa": 10000000} 
#printing in json format
print(json.dumps(population, indent=1))
Output : 
Odisha: 40000000 
Telangana: 50000000 
Delhi: 80000000 
Goa: 10000000