How to print keys and values of a python dictionary

Python program to print dictionary keys and values :

In this tutorial, we will learn how to print the keys and values of a dictionary in Python. For printing the keys and values, we can either iterate through the dictionary one by one and print all key-value pairs or we can print all keys or values in one go. For this tutorial, we are using Python 3.

This is the simplest way to print all key-value pairs of a Python dictionary. With one for loop, we can iterate through the keys of the dictionary one by one and then print the keys with their associated value. To access the value of a key k of a dictionary dic, we can use square braces like dic[k].

The following example will print the key-value pairs:

my_dict = {"one": 1,"two":2,"three":3,"four":4}

for item in my_dict:
    print("Key : {} , Value : {}".format(item,my_dict[item]))

This program is iterating through the keys of the dictionary my_dict. On each iteration of the loop, it will access the value of the current key item as like my_dict[item].

Key : one , Value : 1
Key : two , Value : 2
Key : three , Value : 3
Key : four , Value : 4

As you can see, it printed the keys and values of the dictionary my_dict.

python print dictionary using for loop

Get the code on Github

By using the items() method :

The items() method of Python dictionary returns a view object of the dictionary. It contains the key-value pairs of the dictionary as tuples in a list. For example,

my_dict = {"one": 1,"two":2,"three":3,"four":4}

print(my_dict.items())

This program will print:

dict_items([('one', 1), ('two', 2), ('three', 3), ('four', 4)])

We can iterate over these items of the list to print out the key-value pairs.

my_dict = {"one": 1,"two":2,"three":3,"four":4}

for key,value in my_dict.items():
    print("Key : {} , Value : {}".format(key,value))

If you run this program, it will print a similar output. python print dictionary using items method

Get the code on Github

By iterating through the keys :

Python dictionary provides the keys() method to get all keys from a Python dictionary. Then we can iterate through the keys one by one and print out the value for each key.

The keys() method returns a view object holding the keys of the dictionary. For example:

my_dict = {"one": 1,"two":2,"three":3,"four":4}

print(my_dict.keys())

This program will print:

dict_keys(['one', 'two', 'three', 'four'])

We can use a for loop to iterate over the keys of a dictionary and for each key, we can use the curly braces to print the values. The following example prints the keys and values of the dictionary my_dict by using the keys() method.

my_dict = {"one": 1,"two":2,"three":3,"four":4}

for key in my_dict.keys():
    print("Key : {} , Value : {}".format(key,my_dict[key]))

If you run this program, it will print the same output. python print dictionary iterating keys

Get the code on Github

How to print the first and last N key-value pairs of a dictionary:

We can use list slicing to print the first and last N key-value pairs of a Python dictionary. The syntax of list slicing is:

list[start:stop:step]

Here,

  • start is the index to start the slicing.
  • stop is the index to stop the slicing.
  • step is the step size.

We can only use the start and stop parameters to get the first and last N items of a list. For example,

given_list = [1, 2, 3, 4, 5, 6, 7]

print(given_list[3:])
print(given_list[:4])

Here, given_list[3:] will return one list with items from index 3 to the end of the list and given_list[:4] will return one list with items from the start to index 4.

We can use the list() method to convert the items of a dictionary into a list. The dict.items() method returns the items, and list(dict.items()) will return the list of all dictionary items.

For example, the following program will print the first two key-value pairs of the dictionary my_dict:

my_dict = {"one": 1, "two": 2, "three": 3, "four": 4}

dict_list = list(my_dict.items())[:2]

for k, v in dict_list:
    print(f"Key: {k}, Value: {v}")

It will print:

Key: one, Value: 1
Key: two, Value: 2

Get the code on Github

Similarly, the following program will print the last three key-value pairs of the dictionary:

my_dict = {"one": 1, "two": 2, "three": 3, "four": 4}

dict_list = list(my_dict.items())[1:]

for k, v in dict_list:
    print(f"Key: {k}, Value: {v}")

It will print the items starting from index 1 to the end of the list:

Key: two, Value: 2
Key: three, Value: 3
Key: four, Value: 4

Get the code on Github

Conclusion :

You can use any of the above methods to iterate through the key-value pairs of a Python dictionary. We can use string slicing to print any N number of dictionary elements.

Similar tutorials :