How to truncate a Python dictionary at a given length?



Truncating a dictionary in Python means limiting it to a fixed number of key-value pairs.

From version Python 3.7 onwards, the dictionaries store the values based on the insertion order, which makes it easy to slice and rebuild a smaller dictionary using the first N items.

This is helpful when we want to reduce data size or process only a portion of the dictionary.

In this article, we are going to explore the different methods to truncate a Python dictionary at a given length.

Using itertools.islice() Method

The itertools.islice() method in Python is used to truncate a dictionary by slicing its items without returning the sliced items into a list. Following is the syntax of using the itertools.islice() method -

itertools.islice(sequence, stop)
or
itertools.islice(sequence, start, stop, step)

Where,

  • sequence parameter is the item we want to iterate.
  • stop is to stop the iteration at a specific position.
  • start is where the iteration begins.
  • step helps in skipping the items.

Note: The islice() method does not support negative values for start, stop, or step.

Example 1: Truncate a Dictionary at a Given Length

In this example, we will truncate a dictionary with four key:value pairs to two using the itertools module's islice() method -

import itertools

# Creating a Dictionary with 4 key-value pairs
myprod = {
	"Product":"Mobile",
	"Model": "XUT",
	"Units": 120,
	"Available": "Yes"
}

# Displaying the Dictionary
print("Dictionary =\n",myprod)

# Truncating a Dictionary using the islice()
myprod = dict(itertools.islice(myprod.items(),2))

# Displaying the Dictionary
print("Dictionary after truncating =\n",myprod)

Here is the output of the above program -

Dictionary =
 {'Product': 'Mobile', 'Model': 'XUT', 'Units': 120, 'Available': 'Yes'}
Dictionary after truncating =
 {'Product': 'Mobile', 'Model': 'XUT'}

Example 2: Truncate a Dictionary in a Given Range

Here in this example, we will truncate a dictionary within a range by defining the start and stop parameters in the islice() method -

import itertools

# Creating a Dictionary with 6 key-value pairs
myprod = {
	"Product":"Mobile",
	"Model": "XUT",
	"Units": 120,
	"Available": "Yes",
	"Grade": "A",
	"Rating": "5"
}

# Displaying the Dictionary
print("Dictionary =\n",myprod)

# Truncating a Dictionary using the islice()
# We have set the parameters start and stop to truncate in a range
myprod = dict(itertools.islice(myprod.items(),3,5))

# Displaying the Dictionary
print("Dictionary after truncating =\n",myprod)

Following is the output of the above example -

Dictionary =
 {'Product': 'Mobile', 'Model': 'XUT', 'Units': 120, 'Available': 'Yes', 'Grade': 'A', 'Rating': '5'}
Dictionary after truncating =
 {'Available': 'Yes', 'Grade': 'A'}

Example 3: Truncate a Dictionary by Skipping the Items

Here in this example, we are using the method islice() by defining the start, stop, and step parameters to skip the number of items defined within the range -

import itertools
# Creating a Dictionary with 6 key-value pairs
myprod = {
	"Product":"Mobile",
	"Model": "XUT",
	"Units": 120,
	"Available": "Yes",
	"Grade": "A",
	"Rating": "5"
}

# Displaying the Dictionary
print("Dictionary =\n",myprod)

# Truncating a Dictionary using the islice()
# We have set the parameters start, stop and step to skip items
myprod = dict(itertools.islice(myprod.items(),2,5,2))

# Displaying the Dictionary
print("Dictionary after truncating =\n",myprod)

Below is the output of the above example -

Dictionary =
 {'Product': 'Mobile', 'Model': 'XUT', 'Units': 120, 'Available': 'Yes', 'Grade': 'A', 'Rating': '5'}
Dictionary after truncating =
 {'Units': 120, 'Grade': 'A'}

Using dict() with list() and items()

Another method to truncate a dictionary is by converting the dictionary items to a list, slicing the list, and then converting it back to a dictionary.

In this method, we will use the methods dict(), list(), and items combinedly to truncate the dictionary. Here is the syntax of using dict(), list() and items() methods to truncate a dictionary -

dict(list(dictionary.items())[:n])

Where,

n: The number of key-value pairs to retain.

Example

Following is the example of using the methods dict(), list(), and items combinedly to truncate the defined number of items from a dictionary -

# Original dictionary
data = {'name': 'Alice', 'age': 30, 'city': 'New York', 'country': 'USA'}

# Truncate to 2 items
truncated = dict(list(data.items())[:2])

print("Truncated Dictionary:", truncated)

Here is the output of the above example -

Truncated Dictionary: {'name': 'Alice', 'age': 30}
Updated on: 2025-06-10T17:47:50+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements