Print only a part of a dictionary in Python

avatar

Borislav Hadzhiev

Last updated: Sep 15, 2022

banner

Photo from Unsplash

Print only a part of a dictionary in Python #

To only print a part of a dictionary:

  1. Use the dict.items() method to get a view of the dictionary's items.
  2. Convert the view to a list and use list slicing to get a part of the dictionary.
  3. Use the print() function to print the result.
main.py
my_dict = { 'id': 1, 'age': 30, 'salary': 100, 'name': 'bobbyhadz', 'language': 'Python' } result = dict(list(my_dict.items())[0:3]) print(result) # 👉️ {'id': 1, 'age': 30, 'salary': 100}

The dict.items method returns a new view of the dictionary's items ((key, value) pairs).

main.py
my_dict = {'id': 1, 'name': 'BobbyHadz'} print(my_dict.items()) # 👉️ dict_items([('id', 1), ('name', 'BobbyHadz')])
We used the list() class to convert the view object to a list and used list slicing to select the first 3 items in the dictionary.

The syntax for list slicing is my_list[start:stop:step].

The start index is inclusive and the stop index is exclusive (up to, but not including).

Python indexes are zero-based, so the first item in a list has an index of 0, and the last item has an index of -1 or len(my_list) - 1.

If you need to exclude certain keys from the dictionary and print the result, use a dict comprehension.

main.py
my_dict = { 'id': 1, 'age': 30, 'salary': 100, 'name': 'bobbyhadz', 'language': 'Python' } def exclude_keys(dictionary, keys): return { key: value for key, value in dictionary.items() if key not in keys } result = exclude_keys(my_dict, ['id', 'age']) # 👇️ {'salary': 100, 'name': 'bobbyhadz', 'language': 'Python'} print(result)

We used a dict comprehension to iterate over the dictionary's items and excluded the specified keys.

Dict comprehensions are very similar to list comprehensions.

They perform some operation for every key-value pair in the dictionary or select a subset of key-value pairs that meet a condition.

You can tweak the dict comprehension if you only need to select specific key-value pairs.

main.py
my_dict = { 'id': 1, 'age': 30, 'salary': 100, 'name': 'bobbyhadz', 'language': 'Python' } def include_keys(dictionary, keys): return { key: value for key, value in dictionary.items() if key in keys } result = include_keys(my_dict, ['id', 'age', 'name']) print(result) # 👉️ {'id': 1, 'age': 30, 'name': 'bobbyhadz'}

The include_keys() function takes a dictionary and a list of keys and returns a new dictionary that only contains the specified key-value pairs.

You can also use the itertools.islice() method to only print a part of a dictionary.

main.py
from itertools import islice my_dict = { 'id': 1, 'age': 30, 'salary': 100, 'name': 'bobbyhadz', 'language': 'Python' } result = dict(islice(my_dict.items(), 0, 3)) print(result) # 👉️ {'id': 1, 'age': 30, 'salary': 100}

The itertools.islice method takes an iterable and the start and stop indexes and returns the specified elements from the iterable.

This approach is very similar to the list slicing example but is a little more performant for larger lists.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.