I have a dictionary whose values are a list:
dict ={10: ['jhon', 'abc', '[email protected]'], 12: ['raghav', 'awdaw', '[email protected]']}
Now I am accessing them like this:
dict[10][0]
But It makes so confusing as I don't know what is at index 0 in my list for a particular key.
For this I have another list which helps me what is at index i.
['Manager Name', 'Owner Name', 'Email']
So know this makes easy to access the values of my dictionary. Like I want to use: dict[10]['Manager Name'] instead of dict[10][0]
Is this achievable in Python. Because I tried reference from Map two list in to dictionary But how do I map a dictionary values to a list
Yes, it is possible. Just restructure your dictionary into a nested dictionary:
d_input = {10: ['jhon', 'abc', '[email protected]'],
12: ['raghav', 'awdaw', '[email protected]']}
d = {k: {'Manager Name': a, 'Owner Name': b, 'Email': c} \
for k, (a, b, c) in d_input.items()}
Result:
{10: {'Email': '[email protected]', 'Manager Name': 'jhon', 'Owner Name': 'abc'},
12: {'Email': '[email protected]', 'Manager Name': 'raghav', 'Owner Name': 'awdaw'}}
Extendible version with zip:
cats = ['Manager Name', 'Owner Name', 'Email']
d = {k: dict(zip(cats, v)) for k, v in d_input.items()}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With