Python Map Implementation

Python Map Implementation

Instructor-svgAl-Mamun Sarkar
Mar 28 , 2020

Python Map Implementation. The following code shows how to work with a map in the Python programming language. 

Code:

import collections

my_dict = {'day1': 'Monday', 'day2': 'Tuesday'}
py_dict = {'day3': 'Wednesday', 'day1': 'Thursday'}

res = collections.ChainMap(my_dict, py_dict)

# Creating a single dictionary
print(res.maps, '\n')

print('Keys = {}'.format(list(res.keys())))
print('Values = {}'.format(list(res.values())))
print()

print('elements:')
for key, val in res.items():
    print('{} = {}'.format(key, val))
print()


# Find a specific value in the result
print('day3 in res: {}'.format(('day3' in res)))
print('day4 in res: {}'.format(('day4' in res)))


res2 = collections.ChainMap(py_dict, my_dict)
print(res2.maps, '\n')

 

Output:

[{'day1': 'Monday', 'day2': 'Tuesday'}, {'day3': 'Wednesday', 'day1': 'Thursday'}] 

Keys = ['day3', 'day1', 'day2']
Values = ['Wednesday', 'Monday', 'Tuesday']

elements:
day3 = Wednesday
day1 = Monday
day2 = Tuesday

day3 in res: True
day4 in res: False
[{'day3': 'Wednesday', 'day1': 'Thursday'}, {'day1': 'Monday', 'day2': 'Tuesday'}]

 

  • Share On:
  • fb
  • twitter
  • pinterest
  • instagram