How to Slice a Dictionary in Python, With Examples

Python Dictionary slice()

We’ve covered how to use the slice function to slice strings, tuples, lists, and other sequences. This tutorial will show you how to slice dictionaries, with examples.

Dictionaries

Not sure what a dictionary is in the Python programming language? Get up to speed with our article here.

The slice() Function and the islice() Function

Check out the full article here on how the slice() function works – we’ve covered it in-depth.

Unfortunately, the slice() function built-in to Python does not work with dictionaries.

If you do try to use slice() with a dictionary type variable, you will receive the following error:

TypeError: unhashable type: 'slice'

However, the islice() function from the itertools library does work!

itertools.islice() Python Syntax

The syntax for the itertools.islice() function is as follows:

itertools.islice(ITERABLE, START, STOP, STEP)

Note that:

  • ITERABLE can be any variable or value which can be looped over
    • E.g., a listtuplestring, or in this case, dictionary
  • START is the position (index) you wish to begin the slice at
    • The value at this position is included in the resulting slice
  • END is the position (index) you want to end the slice at
    • The value at this position is NOT included in the resulting slice
  • Negative START/END values are not supported
  • STEP is an optional value that will set how many positions are incremented when slicing
    • E.g., a STEP value of 2 will mean that only values appearing at even indexes in the slice will be included in the result

Slicing Dictionaries using itertools.islice()

Below is a simple example of slicing a Python dictionary. I’ll leave commentary in the code so that you can see what’s going on:

# Import the itertools libarary 
# This library ships with Python 3 so you don't have to download anything
# It provides a bunch of tools for dealing with sequences and iteration
import itertools

# Create a new dictionary variable
myDictionary = {
    "name": "Fred",
    "animal": "cat",
    "colour": "red",
    "age": 3
} 

# Create a sliced dictionary
# dict() is used to convert the iterable result of itertools.islice() to a new dictionary
slicedDict = dict(itertools.islice(myDictionary.items(), 1 ,3))

print(slicedDict)

This will result in the following output:

{   
    'animal': 'cat', 
    'colour': 'red'
}

Dictionaries are ordered – the items in a dictionary are stored in the order they were added – so the islice() function returns its slice based on that order.

SHARE:
nv-author-image

Brad Morton

I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Reply

Your email address will not be published. Required fields are marked *