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:
Implementation of Hashing with Chaining in Python
Next article icon

Implementation of Hashing with Chaining in Python

Last Updated : 10 Jan, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

Concept of Hashing, Hash Table and Hash Function

Hashing is an important Data Structure which is designed to use a special function called the Hash function which is used to map a given value with a particular key for faster access of elements. The efficiency of mapping depends of the efficiency of the hash function used. Example:
h(large_value) = large_value % m
Here, h() is the required hash function and 'm' is the size of the hash table. For large values, hash functions produce value in a given range. hashing How Hash Function Works?
  • It should always map large keys to small keys.
  • It should always generate values between 0 to m-1 where m is the size of the hash table.
  • It should uniformly distribute large keys into hash table slots.
  • Collision Handling

    If we know the keys beforehand, then we have can have perfect hashing. In perfect hashing, we do not have any collisions. However, If we do not know the keys, then we can use the following methods to avoid collisions:
    • Chaining
    • Open Addressing (Linear Probing, Quadratic Probing, Double Hashing)

    Chaining

    While hashing, the hashing function may lead to a collision that is two or more keys are mapped to the same value. Chain hashing avoids collision. The idea is to make each cell of hash table point to a linked list of records that have same hash function value. chain-hashing-11 Note: In Linear Probing, whenever a collision occurs, we probe to the next empty slot. While in Quadratic Probing, whenever a collision occurs, we probe for i^2th slot in the ith iteration and we keep probing until an empty slot in the hashtable is found.

    Performance of Hashing

    The performance of hashing is evaluated on the basis that each key is equally likely to be hashed for any slot of the hash table.
  m = Length of Hash Table  n = Total keys to be inserted in the hash table     Load factor lf = n/m   Expected time to search = O(1 +lf )  Expected time to insert/delete = O(1 + lf)    The time complexity of search insert and delete is   O(1) if  lf is O(1)  
Python Implementation of Hashing Python3 1==
# Function to display hashtable def display_hash(hashTable):          for i in range(len(hashTable)):         print(i, end = " ")                  for j in hashTable[i]:             print("-->", end = " ")             print(j, end = " ")                      print()  # Creating Hashtable as  # a nested list. HashTable = [[] for _ in range(10)]  # Hashing Function to return  # key for every value. def Hashing(keyvalue):     return keyvalue % len(HashTable)   # Insert Function to add # values to the hash table def insert(Hashtable, keyvalue, value):          hash_key = Hashing(keyvalue)     Hashtable[hash_key].append(value)  # Driver Code insert(HashTable, 10, 'Allahabad') insert(HashTable, 25, 'Mumbai') insert(HashTable, 20, 'Mathura') insert(HashTable, 9, 'Delhi') insert(HashTable, 21, 'Punjab') insert(HashTable, 21, 'Noida')  display_hash (HashTable) 
Output:
0 --> Allahabad --> Mathura   1 --> Punjab --> Noida   2   3   4   5 --> Mumbai   6   7   8   9 --> Delhi 

Next Article
Implementation of Hashing with Chaining in Python

S

simranjenny84
Improve
Article Tags :
  • Python
  • Hash
  • python-list
  • Python-DSA
Practice Tags :
  • Hash
  • python
  • python-list

Similar Reads

    Implementation of Hash Table in Python using Separate Chaining
    A hash table is a data structure that allows for quick insertion, deletion, and retrieval of data. It works by using a hash function to map a key to an index in an array. In this article, we will implement a hash table in Python using separate chaining to handle collisions. Components of hashing Sep
    7 min read
    Implementation of Hash Table in C/C++ using Separate Chaining
    Introduction: Hashing is a technique that maps a large set of data to a small set of data. It uses a hash function for doing this mapping. It is an irreversible process and we cannot find the original value of the key from its hashed value because we are trying to map a large set of data into a smal
    10 min read
    Implementing our Own Hash Table with Separate Chaining in Java
    All data structure has their own special characteristics, for example, a BST is used when quick searching of an element (in log(n)) is required. A heap or a priority queue is used when the minimum or maximum element needs to be fetched in constant time. Similarly, a hash table is used to fetch, add
    10 min read
    Internal implementation of Data Structures in Python
    Python provides a variety of built-in data structures, each with its own characteristics and internal implementations optimized for specific use cases. In this article we are going to discuss about the most commonly used Data structures in Python and a brief overview of their internal implementation
    3 min read
    Python 3.6 Dictionary Implementation using Hash Tables
    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 Dictiona
    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