3

I have a dictionary whose values are a list:

dict ={10: ['jhon', 'abc', 'wa@gmail.com'], 12: ['raghav', 'awdaw', 'raghavpatnecha15@gmail.com']}

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

Raghav Patnecha
  • 716
  • 8
  • 29

5 Answers5

3

Yes, it is possible. Just restructure your dictionary into a nested dictionary:

d_input = {10: ['jhon', 'abc', 'wa@gmail.com'],
           12: ['raghav', 'awdaw', 'raghavpatnecha15@gmail.com']}

d = {k: {'Manager Name': a, 'Owner Name': b, 'Email': c} \
        for k, (a, b, c) in d_input.items()}

Result:

{10: {'Email': 'wa@gmail.com', 'Manager Name': 'jhon', 'Owner Name': 'abc'},
 12: {'Email': 'raghavpatnecha15@gmail.com', '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()}
jpp
  • 159,742
  • 34
  • 281
  • 339
  • what if the length of my list and the dictionary value list is variable.Lets say in this example the length is 3 but it can be 8 if I add more parameters. I don't want to add more (a,b,c,d,e,f,g,h). – Raghav Patnecha Apr 11 '18 at 09:23
3

This is a clear use case for classes.

Instead of having lists that represent companies like ['jhon', 'abc', 'wa@gmail.com'], python gives you the possibility to model that as a class:

class Company:
    def __init__(self, manager_name, owner_name, email):
        self.manager_name = manager_name
        self.owner_name = owner_name
        self.email = email

companies = {10: Company('jhon', 'abc', 'wa@gmail.com'),
             12: Company('raghav', 'awdaw', 'raghavpatnecha15@gmail.com'}

This gives you a much nicer interface than a list, because you can access the attributes by name rather than by index:

>>> comp = Company('jhon', 'abc', 'wa@gmail.com')
>>> comp.manager_name
'jhon'
>>> comp.owner_name
'abc'
>>> comp.email
'wa@gmail.com'

If you're lazy and don't want to define the Company class manually, you can use collections.namedtuple to reduce the amount of code you have to write:

from collections import namedtuple

Company = namedtuple('Company', 'manager_name owner_name email')

You can also convert your dict of lists to a dict of Company instances easily:

companies = {10: ['jhon', 'abc', 'wa@gmail.com'],
             12: ['raghav', 'awdaw', 'raghavpatnecha15@gmail.com']}

comps = {num: Company(*comp) for num, comp in companies.items()}
# result:
# {10: Company(manager_name='jhon', owner_name='abc', email='wa@gmail.com'),
#  12: Company(manager_name='raghav', owner_name='awdaw', email='raghavpatnecha15@gmail.com')}
Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
2

If your "attributes" are always exactly ['Manager Name', 'Owner Name', 'Email'], you should consider using namedtuples or the new dataclass of Python 3.7.

Here is an example using namedtuples:

from collections import namedtuple

Company = namedtuple('Company', ['manager', 'owner', 'email'])

comp = Company('jhon', 'abc', 'wa@gmail.com')
print(comp.manager, comp.owner, comp.email)

results in jhon abc wa@gmail.com

Guybrush
  • 2,680
  • 1
  • 10
  • 17
1

You should use named tuples.

Your code could look like this:

from collections import namedtuple

Company = namedtuple('Company', 'ManagerName OwnerName Email')

c1 = Company('jhon', 'abc', 'wa@gmail.com')
c2 =  Company('raghav', 'awdaw', 'raghavpatnecha15@gmail.com')

companies = {10: c1, 12: c2}
companies[10].Email
pilu
  • 720
  • 5
  • 16
1

If you don't want to change the dictionary, you can use another dictionary to map the indices to the corresponding values.

>>> d = {10: ['jhon', 'abc', 'wa@gmail.com'], 12: ['raghav', 'awdaw', 'raghavpatnecha15@gmail.com']}    
>>> mapper = {'Manager Name': 0, 'Owner Name': 1, 'Email': 2}    
>>> d[10][mapper['Manager Name']]
'jhon'
>>> d[12][mapper['Email']]
'raghavpatnecha15@gmail.com'
Keyur Potdar
  • 7,158
  • 6
  • 25
  • 40