How to Slice a Dictionary in Python?

In this tutorial, I will explain how to slice a dictionary in Python. Recently during a Python webinar, someone asked me a question about slicing a dictionary in Python which made me explore more about this topic. In this tutorial, I will share my findings with suitable examples and screenshots.

Slice a Dictionary in Python

Dictionary slicing refers to the process of extracting a portion of a dictionary based on certain conditions. Unlike slicing and indexing in Python for sequences like lists, tuples, and strings, dictionary slicing is not a built-in feature. Instead, we need to use various techniques to achieve the desired result.

Read How to Convert a Dictionary to an Array in Python?

Method 1. Use a List of Keys

One common approach to slicing a dictionary is by using a list of keys in Python. If you have a predefined list of keys that you want to extract from the dictionary, you can use a simple dictionary comprehension or a for loop.

Example:

# Original dictionary
person = {
    'name': 'John Doe',
    'age': 30,
    'city': 'New York',
    'country': 'USA',
    'occupation': 'Engineer'
}

# List of keys to extract
keys_to_extract = ['name', 'age', 'city']

# Slicing the dictionary using a dictionary comprehension
sliced_dict = {key: person[key] for key in keys_to_extract}

print(sliced_dict)

Output:

{'name': 'John Doe', 'age': 30, 'city': 'New York'}

I executed the above example code and added the screenshot below.

Slice a Dictionary in Python

This approach is particularly useful when you need to focus on specific data within a larger dictionary, such as extracting user details from a profile or filtering configuration settings. By specifying the keys of interest, you can create a new dictionary that contains only the relevant key-value pairs.

Check out How to Concat Dict in Python?

Method 2. Use Conditional Statements

Another way to slice a dictionary is by using conditional statements in Python. You can iterate over the dictionary items and include only the key-value pairs that satisfy a specific condition.

Example:

# Original dictionary
student_grades = {
    'Alice': 85,
    'Bob': 92,
    'Charlie': 78,
    'David': 90,
    'Eva': 88
}

# Slicing the dictionary based on a condition
passed_students = {name: grade for name, grade in student_grades.items() if grade >= 80}

print(passed_students)

Output:

{'Alice': 85, 'Bob': 92, 'David': 90, 'Eva': 88}

I executed the above example code and added the screenshot below.

How to Slice a Dictionary in Python

This approach is particularly useful for filtering data within a dictionary based on specific criteria. By incorporating a condition within the dictionary comprehension, you can efficiently create a new dictionary containing only the key-value pairs that satisfy the given condition.

Read How to Get the Length of a Dictionary in Python?

Slice Lists within a Dictionary in Python

In some cases, you may have a dictionary where the values are listed in Python. You can slice the lists within the dictionary to access specific items.

Example:

# Original dictionary
employee_data = {
    'John': ['Manager', 5000],
    'Emma': ['Developer', 7000],
    'Michael': ['Designer', 6000]
}

# Slicing the lists within the dictionary
employee_roles = {name: data[0] for name, data in employee_data.items()}

print(employee_roles)

Output:

{'John': 'Manager', 'Emma': 'Developer', 'Michael': 'Designer'}

I executed the above example code and added the screenshot below.

Slicing Lists within a Dictionary in Python

By utilizing dictionary comprehensions, you can efficiently extract specific elements from lists within a dictionary. In this example, we created a new dictionary, employee_roles, that maps each employee’s name to their role by selecting the first element (data[0]) from each list in the original employee_data dictionary.

Check out How to Save Python Dictionary to a CSV File?

Conclusion

In this article, I helped you to learn how to slice a dictionary in Python. I discussed mainly three methods to achieve this task such as using list of keys , using a conditional statement. I also covered slicing lists within a dictionary in Python.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.