Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python 3.6 Dictionary Implementation using Hash Tables
Next article icon

Python 3.6 Dictionary Implementation using Hash Tables

Last Updated : 16 Dec, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

Dictionary in Python is a collection of data values, used to store data values like a map, which, unlike other Data Types that hold only a single value as an element, Dictionary holds key:value pair. Key-value is provided in the dictionary to make it more optimized. Each key-value pair in a Dictionary is separated by a colon :, whereas each key is separated by a ‘comma’. To know more about dictionaries click here. 

Based on a proposal by Raymond Hettinger the new dict() function has 20% to 25% less memory usage compared to python v.3.5 or less. It relies upon the order-preserving semantics proposed by Raymond Hettinger. This implementation makes the dictionaries more compact and provides a faster iteration over them. 

The memory layout of dictionaries in earlier versions was unnecessarily inefficient. It comprised of a sparse table of 24-byte entries that stored the hash value, the key pointer, and the value pointer. The memory layout of dictionaries in version 3.5 and less were implemented to store in a single sparse table. 

Example: 

for the below dictionary:  d = {'banana':'yellow', 'grapes':'green', 'apple':'red'}  used to store as:      entries = [['--', '--', '--'],                [-5850766811922200084, 'grapes', 'green'],                ['--', '--', '--'],                ['--', '--', '--'],                ['--', '--', '--'],                [2247849978273412954, 'banana', 'yellow'],                ['--', '--', '--'],                [-2069363430498323624, 'apple', 'red']]

Instead, in the new dict() implementation the data is now being organized in a dense table referenced by a sparse table of indices as follows: 

 indices  = [None, 1, None, None, None, 0, None, 2]  entries = [[2247849978273412954, 'banana', 'yellow']              [-5850766811922200084, 'grapes', 'green'],              [-2069363430498323624, 'apple', 'red']]


It is important to notice that in the new dict() implementation only the data layout has been changed and no changes are made in the hash table algorithms. Neither the collision statistics nor the table search order has been changed. 

This new implementation of dict() is believed to significantly compress dictionaries for memory saving depending upon the size of the getdictionary. Small dictionaries gets the most benefit out of it. 

For a sparse table of size t with n entries, the sizes are: 

curr_size = 24 * t new_size = 24 * n + sizeof(index) * t


In the above example banana/grapes/apple, the size of the former implementation is 192 bytes ( eight 24-byte entries) and the later implementation has a size of 90 bytes ( three 24-byte entries and eight 1-byte indices ). That shows around 58% compression in size of the dictionary. 

In addition to saving memory, the new memory layout makes iteration faster. Now functions like Keys(), items(), and values can loop over the dense table without having to skip empty slots, unlike the older implementation. Other benefits of this new implementation are better cache utilization, faster resizing and fewer touches to the memory.
 


Next Article
Python 3.6 Dictionary Implementation using Hash Tables

R

RajuKumar19
Improve
Article Tags :
  • Python
  • Python-datatype
Practice Tags :
  • python

Similar Reads

    Sparse Matrix in Python using Dictionary
    A sparse matrix is a matrix in which most of the elements have zero value and thus efficient ways of storing such matrices are required. Sparse matrices are generally utilized in applied machine learning such as in data containing data-encodings that map categories to count and also in entire subfie
    2 min read
    How to maintain dictionary in a heap in Python ?
    Prerequisites: Binary heap data structureheapq module in PythonDictionary in Python. The dictionary can be maintained in heap either based on the key or on the value. The conventions to be maintained are listed below: The key-value pair at index 'i' is considered to be the parent of key-value pair a
    9 min read
    Implementation of Hashing with Chaining in Python
    Hashing is a data structure that is used to store a large amount of data, which can be accessed in O(1) time by operations such as search, insert and delete. Various Applications of Hashing are: Indexing in database Cryptography Symbol Tables in Compiler/Interpreter Dictionaries, caches, etc. Concep
    3 min read
    Bidirectional Hash table or Two way dictionary in Python
    We know about Python dictionaries in a data structure in Python which holds data in the form of key: value pairs. In this article, we will discuss the Bidirectional Hash table or Two-way dictionary in Python. We can say a two-way dictionary can be represented as key ⇐⇒ value. One example of two-way
    3 min read
    How to use a List as a key of a Dictionary in Python 3?
    In Python, we use dictionaries to check if an item is present or not . Dictionaries use key:value pair to search if a key is present or not and if the key is present what is its value . We can use integer, string, tuples as dictionary keys but cannot use list as a key of it . The reason is explained
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences