0
new_dict={}
d1={'name1':'Jaison','name3':'Eldose','name2':'Manu'}
d2={'age1':29,'age2':26,'age3':28}
d3={'job1':'NJ','job2':'SE','job3':'SE'}
def Combine_dictionaries(*args):
    for arg in args:
        for i,j in arg.items():
            print(i,j)
            new_dict[i]=[j]
    return new_dict  

print(Combine_dictionaries(d1,d2,d3))
print(new_dict.items())

out put:

dict_items([('name1', ['Jaison']), ('name3', ['Eldose']), ('name2', ['Manu']), ('age1', [29]), ('age2', [26]), ('age3', [28]), ('job1', ['NJ']), ('job2', ['SE']), ('job3', ['SE'])])

Question: Is there any suggestions to improve the output ?

codeforester
  • 39,467
  • 16
  • 112
  • 140
JaisoN
  • 474
  • 1
  • 4
  • 12
  • https://docs.python.org/3/whatsnew/3.5.html#pep-448-additional-unpacking-generalizations – benvc Jan 24 '20 at 15:48

1 Answers1

3

My personal opinion is that this probably shouldn't be a dictionary. I wouldn't index into a dictionary looking for 'name1' in all likelihood. I would make this lists, maybe like so.

names = ['Jaison', 'Eldose', 'Manu'] # then names[0] = 'Jaison'
ages = [29, 26, 28]
jobs = ['NJ', 'SE', 'SE']

To combine them we could either have a list of tuples of a list of dictionaries.

people_list = [(names[i], ages[i], jobs[i]) for i in range(len(names))]
# [('Jaison', 29, 'NJ'), ('Eldose', 26, 'SE'), ('Manu', 28, 'SE')]
people_dict = [{'name':names[i], 'age':ages[i], 'job':jobs[i]} for i in range(len(names))]
#[{'name': 'Jaison', 'age': 29, 'job': 'NJ'}, {'name': 'Eldose', 'age': 26, 'job': 'SE'}, {'name': 'Manu', 'age': 28, 'job': 'SE'}]

Just a suggestion though. Hope it helps!

cmxu
  • 954
  • 5
  • 13