Open In App

Map Abstract Data Type

Last Updated : 20 Jan, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

ADT stands for Abstract data type. This data type is defined by the programmer. Abstract data type contains a set of well-defined values and operations. ADT acts as an asset in programming as its implementation can be changed without modifying the rest of the program. With the help of an abstract data type, we can also reduce logical errors. Owing to the fact that now you have got the gist of abstract data type, let us now move forward to our topic which is the Map Abstract Data Type.

Map Abstract Data Type

The map is an abstract data type that contains a collection of records. It is an interface, that states what all operations can be performed, but not their implementation. Every record of a map contains a key and a value. The property of the map is such that every value can be accessed or retrieved with the help of a key. This means that every key is associated with a value. It is very important to note that every key in the map has to be unique. Values can be duplicated but the keys have to be unique every time. In various programming languages, maps have different names for their implementation. Look at the example below:

  1. C++ : unordered_map, multimap, hash_map,etc. are the implementations.
  2. Python: dictionaries.
  3. Java: HashMaps, HashTables.

Operations of Map

Let us now get acquainted with some of the useful operations that one can perform using a map:

  • put(key, value): A new key-value pair is added to the map. In case, if the key already exists in the map then the old value is replaced by the new.
  • get(key): A key is passed here and with the help of the key we can retrieve the value which is associated with the given key.
  • len(): This operation returns the length of the map i.e. the number of key-value pairs.
  • del: This operation is used to delete a specific key-value pair from the map. This is how it is done: del map[key]. The particular key and its associated value get deleted from the map.
  • in: This operation returns a boolean value. It returns true if the given key exists in the map, else returns false.

Uses of Map

In the following situations, a map abstract data type is ideal to use:

  • Marks of a student by his/her student id.
  • Tax data by social security number.
  • Salary of an employee by his/her staff id.
  • Motorbike owner by owner’s license plate.

Maps are highly used to count frequency as well. For example: Count the duplicate numbers in an array. Here a map can be used where the key would be the index of the number and value would be its frequency. The following table depicts an example of a key-value pair:

Roll No. Name
101 Shawn
102 John
103 David
104 John

In the above table, Roll No. is the key and Name is the value. So you can see that the value ‘John’ has occurred twice but since their keys i.e. the Roll No. are different, they can be stored and identified by the map. So from the above example, it is clear that we only need the key to access or remove any value. An important fact about the map to be known is that it is not mandatory to have the key and value of the same data type. The map allows us to use different data types for key and value respectively.

Implementation of Map

A Map is also known as an associative array. So basically, for implementing a map we need two data values. The initial one is the key with the help of which you can access, delete, replace the value associated with it. Value is the second parameter that is taken while creating a map. We can implement the map using the following two ways:

  • List
  • Dictionary

Let us discuss them one by one.

List:

Now, there are different ways in which a map can be implemented but at this moment, the list seems the best. A list is used to represent the collection or set of values in which the order is very important. So to implement a map using a list we have to create two lists in parallel. Where the first list is used to store the keys and the second list is used to store the values. Have a look at the following example where 2 lists are created.

List 1:

Index Pin code
0 400099
1 500078
2 340005

 

List 2:

Index Road name
0 Diagon Alley
1 Hagrid Rd.
2 Albus Warne 

The above two lists are an example of implementing a map using dual lists. The first list contains key as pin code and the second list contains the name of the road as the value associated with a particular key. 

Example:

Python




# Python program to implement map using dual list
  
# keys
listA = ['one', 'two', 'three']  
  
# values
listB = ['Cricket', 'Football', 'Wrestling']  
  
# map function creates a new array for every 
# element of a calling function.
print(list(map(lambda x, y: x + ' ' + y, listA, listB)))


Output

['one Cricket', 'two Football', 'three Wrestling']

Dictionary

Another way of implementing a map is using the built-in dictionary data type. A dictionary helps us to create a list with keys and values similar to a map. In Python, a dictionary can be created by inserting a collection of elements within curly {} braces, which are separated by ‘comma’. It is important to note that values can change but the keys are immutable. The important feature of a dictionary is that it does not allow duplicates. Another property of the dictionary is that it is ordered in nature while insertion which means that the items have a defined order that cannot be changed. Although dictionaries are changeable which means that we can add, remove, or replace elements even after the dictionary is created. Before understanding how a dictionary can be used to implement a map, let us get a hang of some important dictionary methods.

Method Description
get() This method is used to return the value of the specified key.
clear() This method is used to remove all the elements from the dictionary.
pop() This method is used to remove the element associated with the specified key.
update() This method is used to update the dictionary with the specified key-value pairs.
values() This method is used to return a list of all the values present in the dictionary.
keys This method is used to return a list of all the keys present in the dictionary.

Example:

Python




# Python program to implement map using dictionary
  
# Creating a dictionary
Sports_dict = {
    "Name": "Virat",
    "Sport": "Cricket",
    "Runs": 12000
}
  
# Prints the entire dictionary in key-value pair
print(Sports_dict)
  
# Prints the value associated with the following key
print(Sports_dict["Runs"])


Output

{'Runs': 12000, 'Sport': 'Cricket', 'Name': 'Virat'}
12000

Sample Questions

Question 1: Create a list of the number of goals scored by every player in a team of 6 with their names using a map.

Solution:

Python3




# Python3 program to create a list of the number of 
# goals scored by every player in a team of 6 with 
# their names using a map
  
# Implementing the map using dictionary
Super_FC = {'Chris': 7, 'Mark': 3, 'Paul': 6
            'Adam': 8, 'Micheal': 10, 'Stuart': 12}
  
# Prints the dictionary
print(Super_FC)  


Output

{'Chris': 7, 'Mark': 3, 'Paul': 6, 'Adam': 8, 'Micheal': 10, 'Stuart': 12}

Question 2: Create a list of car and their colors. Find out the color of a specific car.

Solution:

Python3




# Python3 program to create a list of car and 
# their colors. Find out the color of a specific car.
  
# Implementing the map using dictionary
cars = { 'Jaguar': 'Black Color', 'Ferrari': 'Red Color',
         'Mercedes': 'White Color'}
  
# Prints dictionary
print(cars) 
  
# Print the color of Ferari
print(cars['Ferrari'])  


Output

{'Jaguar': 'Black Color', 'Ferrari': 'Red Color', 'Mercedes': 'White Color'}
Red Color

Question 3: Remove a particular sentence from a specific number of the line from a book.

Solution:

Python3




# Python3 program to remove a particular sentence 
# from a specific no. of the line from a book.
  
# Creating book dictionary
book = { 'line1': 'Hello World!', 'line2': 'I am the best',
         'line3': 'The Giant is here!!', 'line4': 'Yes!',
         'line5': 'Big Show'}
print("Before deleting : ")
print(book)
  
# line1 deleted
del book['line1']  
print("After deleting line1 : ")
print(book)


Output

Before deleting : 
{'line1': 'Hello World!', 'line2': 'I am the best', 
 'line3': 'The Giant is here!!', 'line4': 'Yes!', 
 'line5': 'Big Show'}
After deleting line1 : 
{'line2': 'I am the best', 'line3': 'The Giant is here!!', 
 'line4': 'Yes!', 'line5': 'Big Show'}


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads