map data structure in python code example
Example 1: python 3.7 insert at place in dict
def insert_item(dic, item={}, pos=None):
"""
Insert a key, value pair into an ordered dictionary.
Insert before the specified position.
"""
from collections import OrderedDict
d = OrderedDict()
if not item or not isinstance(item, dict):
print('Aborting. Argument item must be a dictionary.')
return dic
if not pos:
dic.update(item)
return dic
for item_k, item_v in item.items():
for k, v in dic.items():
if k == pos:
d[item_k] = item_v
d[k] = v
return d
d = {'A':'letter A', 'C': 'letter C'}
insert_item(['A', 'C'], item={'B'})
insert_item(d, item={'B': 'letter B'})
insert_item(d, pos='C', item={'B': 'letter B'})
Example 2: map function in python
def addition(n):
return n + n
numbers = (1, 2, 3, 4)
result = map(addition, numbers)
print(list(result))
Example 3: dictionary in python
ages = {"John": 43, "Bob": 24, "Ruth": 76}
Example 4: map in python
'''try this instead of defining a function'''
a = [1,2,3,4]
a = list(map(lambda n: n+n, a))
print(a)
Example 5: map in python
map(functions, iterables)
Example 6: hash table in python
dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
name = dict["Name"]