
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Truncate Python Dictionary at Given Length
To truncate a Python Dictionary at a given length, use the itertools module. This module implements a number of iterator building blocks inspired by constructs from APL, Haskell, and SML. For truncating at a given length, we will use the islice() method of the itertools module.
The module standardizes a core set of fast, memory efficient tools that are useful by themselves or in combination. Together, they form an iterator algebra making it possible to construct specialized tools succinctly and efficiently in pure Python.
Syntax
Here's the syntax ?
itertools.islice(sequence, stop) or itertools.islice(sequence, start, stop, step)
Above, the sequence parameter is the item you want to iterate, whereas stop is to stop the iteration at a specific position. The start is where the iteration begins. The step helps in skipping the items. The islice() does not support negative values for start, stop, or step.
Truncate a Dictionary at a given length
We will truncate a dictionary with four key:value pairs to two using the itertools module islice() method. The items are truncated to two and the same we have set as a parameter ?
Example
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)
Output
Dictionary = {'Product': 'Mobile', 'Model': 'XUT', 'Units': 120, 'Available': 'Yes'} Dictionary after truncating = {'Product': 'Mobile', 'Model': 'XUT'}
Truncate a Dictionary in a given range
We will truncate a dictionary with six key:value pairs using the itertools module islice() method. The items are truncated in a range since we have set the start and stop parameters ?
Example
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)
Output
Dictionary = {'Product': 'Mobile', 'Model': 'XUT', 'Units': 120, 'Available': 'Yes', 'Grade': 'A', 'Rating': '5'} Dictionary after truncating = {'Available': 'Yes', 'Grade': 'A'}
Truncate a Dictionary by skipping the items
We will truncate a dictionary with six key:value pairs using the itertools module islice() method. The items are truncated in a range since we have set the start and stop parameters. The items are skipped using the step parameter ?
Example
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)
Output
Dictionary = {'Product': 'Mobile', 'Model': 'XUT', 'Units': 120, 'Available': 'Yes', 'Grade': 'A', 'Rating': '5'} Dictionary after truncating = {'Units': 120, 'Grade': 'A'}