Python Hash Map đēī¸
Hash Map
Hash maps are indexed data structures. A hash map uses a hash function to compute an index into an array of buckets or slots, from which the desired value can be found. In Python, the Dictionary data types represent the implementation of hash tables.
⨠Accessing Values
To access dictionary elements, you can use the familiar square brackets along with the key to obtain its value.
python
dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
print("dict['Name']: ", dict['Name'])
print("dict['Age']: ", dict['Age'])đ Updating Dictionary
You can update a dictionary by adding a new entry or a key-value pair, modifying an existing entry, or deleting an existing entry.
python
dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
dict['Age'] = 8; # update existing entry
dict['School'] = "DPS School"; # Add new entry
print("dict['Age']: ", dict['Age'])
print("dict['School']: ", dict['School'])đŽ Interactive Dictionary Demo
âšī¸ Ready to use Dictionary!
'Name'
'Zara'
Hash: 1
'Age'
'7'
Hash: 5
'Class'
'First'
Hash: 6
đī¸ Delete Dictionary Elements
You can either remove individual dictionary elements or clear the entire contents of a dictionary.
python
dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
del dict['Name']; # remove entry with key 'Name'
dict.clear(); # remove all entries in dict
del dict ; # delete entire dictionary⨠Pro Tip
Hash maps provide fast access to data, with an average time complexity of O(1) for lookups.