Python Program to Print Dictionary (With Example)

In this article, we will learn to write python program to print dictionary.

There are multiple ways to print dictionary in python. Some of them are –

  1. Print Dictionary as Single String
  2. Print Dictionary as (Key : Value) Pair
  3. Print Dictionary as (Key : Value) Pair Using List Comprehension
  4. Print Dictionary as Json String
  5. Print All Keys of Dictionary
  6. Print All Values of Dictionary

We can print dictionary as single string using print() method in python as below –

sampleDictionary = {'w': 1, 'x': 2, 'y': 3, 'z': 4}
print(sampleDictionary)

In above program, we have initialised a dictionary named sampleDictionary. Then, we printed whole dictionary as single string using it’s name and print() method.

When you run above program, it’s output is –

 
{'w': 1, 'x': 2, 'y': 3, 'z': 4}

We can print dictionary as key-value pair in multiple ways. Here, we have covered some of them as below –

  • Method 1

    As we already know, dictionary contains list of key value pairs. If we want to print dictionary as key-value pairs, we can do so using for loop and .items() method associated with dictionary.

    sampleDictionary = {'w': 1, 'x': 2, 'y': 3, 'z': 4}
    for key, value in sampleDictionary.items():
    	print(key, ':', value)
    

    Here,

    1. We have initialised a dictionary, sampleDictionary, and assigned some values to it.
    2. Then, run a for loop to traverse each element one by one present in dictionary.
    3. At each iteration, we get set of key and value stored in variable key and value respectively.
    4. After that, we print key and value using print() method.

    When you run above program, output is –

    w : 1
    x : 2
    y : 3
    z : 4
    
  • Method 2

    At each iteration of for loop, we get key at that index. Once we have key of dictionary, value of that key can be accessed using array indexing like dictionary[key].

    sampleDictionary = { 'w': 1, 'x': 2, 'y': 3, 'z': 4 }
    for key in sampleDictionary:
        print(key, ' : ', sampleDictionary[key])
    

    When we run above program, output is –

    w  :  1
    x  :  2
    y  :  3
    z  :  4
    

Using List comprehension trick can also be applied to print all key and value pairs of dictionary line by line as shown below –

sampleDictionary = { 'w': 1, 'x': 2, 'y': 3, 'z': 4 }
[print(key,':',value) for key, value in sampleDictionary.items()]

When we run above program, output is –

w : 1
x : 2
y : 3
z : 4

There is a method json.dumps() in json module in python that serialize the passed object to json like string. Passing dictionary in json.dumps() returns string that contains values of key-value pair separated by new line.

Let’s take an example,

import json

sampleDictionary = { 'w': 1, 'x': 2, 'y': 3, 'z': 4 }
print(json.dumps(sampleDictionary, indent=4))

Here,

  • We initialised a dictionary with sample key-value pairs.
  • Then, we passed dictionary, sampleDictionary, and indent in json.dumps() method. indent sets count of indent spaces.

When we run above program, output is –

{
    "w": 1,
    "x": 2,
    "y": 3,
    "z": 4
}

Notice that each key-value pair line has 4 spaces.

Till now, we have seen how to write python program to print dictionary
As we already know, dictionary contains sets of key and value pairs. What if we want to print only keys present in dictionary ?

  • Method 1

    We can print keys using keys() method associated with dictionary and for loop as shown below –

    sampleDictionary = {'w': 1, 'x': 2, 'y': 3, 'z': 4}
    for key in sampleDictionary.keys():
    	print(key)
    

    Here,

    1. We initialised a dictionary, sampleDictionary, and assigned values to it.
    2. Using .keys() method, we get all keys present in dictionary as list. Using this list, we run a for loop on it.
    3. At each iteration of for loop, we get key present at that position.
    4. After that, we print value of key using print() method.

    When you run above program, output is –

    w
    x
    y
    z
    

    You can verify these keys present in above program.

  • Method 2

    We can also print keys of dictionary as shown below –

    sampleDictionary = {'w': 1, 'x': 2, 'y': 3, 'z': 4}
    for key in sampleDictionary:
    	print(key)
    

    Notice that we have not used .keys method associated with dictionary. In each iteration of for loop, we get key of dictionary. Then, we print it using print() method.

    When we run above program, output is –

    w
    x
    y
    z
    

We can print all values of dictionary using for loop or .values() method associated with dictionary.

  • Method 1

    If we want to print all values present in dictionary without keys, we can do so using .values() method associated with dictionary.

    sampleDictionary = {'w': 1, 'x': 2, 'y': 3, 'z': 4}
    for value in sampleDictionary.values():
    	print(value)
    

    Here,

    1. We initialised dictionary, sampleDictionary, and assigned values to it.
    2. Then, we get all values of dictionary using .values() method associated with it.
    3. After that, we run a for loop on values of list and print it’s value using print() method.

    When we run above program, output is –

    1
    2
    3
    4
    
  • Method 2

    sampleDictionary = {'w': 1, 'x': 2, 'y': 3, 'z': 4}
    for key in sampleDictionary:
    	print(sampleDictionary[key])
    

    Here, we access values of dictionary using it’s key got at each iteration of for loop.

    When we run above program, output is –

    1
    2
    3
    4
    

That’s how we can write python program to print dictionary, it’s values(without keys), and keys (without values).

Leave a Reply