Hashmap

Hash maps are indexed data structures. A hash map makes use of a hash function to compute an index with a key into an array of buckets or slots. Its value is mapped to the bucket with the corresponding index. The key is unique and immutable. In Python, dictionaries are examples of hash maps.

Program:

Python3




def printdict(d):
    for key in d:
        print(key, "->", d[key])
 
 
hm = {0: 'first', 1: 'second', 2: 'third'}
printdict(hm)
print()
 
hm[3] = 'fourth'
printdict(hm)
print()
 
hm.popitem()
printdict(hm)


Output:

0 -> first

1 -> second

2 -> third

0 -> first

1 -> second

2 -> third

3 -> fourth

0 -> first

1 -> second

2 -> third



User Defined Data Structures in Python

In computer science, a data structure is a logical way of organizing data in computer memory so that it can be used effectively. A data structure allows data to be added, removed, stored and maintained in a structured manner. Python supports two types of data structures:

  • Non-primitive data types: Python has list, set, and dictionary as its non-primitive data types which can also be considered its in-built data structures.
  • User-defined data structures: Data structures that aren’t supported by python but can be programmed to reflect the same functionality using concepts supported by python are user-defined data structures. There are many data structure that can be implemented this way:

Similar Reads

Linked list

A linked list is a linear data structure, in which the elements are not stored at contiguous memory locations. The elements in a linked list are linked using pointers as shown in the below image:...

Stack

...

Queue

A stack is a linear structure that allows data to be inserted and removed from the same end thus follows a last in first out(LIFO) system. Insertion and deletion is known as push() and pop() respectively....

Tree

...

Graph

A queue is a linear structure that allows insertion of elements from one end and deletion from the other. Thus it follows, First In First Out(FIFO) methodology. The end which allows deletion is known as the front of the queue and the other end is known as the rear end of the queue....

Hashmap

...

Contact Us