1

I'm making some scrip with Python and having one small question.

I have 2 lists:

  1. ['name', 'age', 'sex', 'addr', 'city']

  2. ['Jack 24 male no23 NY', 'Jane 25 female no24 NY', 'Dane 14 male no14 NY']

So I want to have:

dictofJack = {'name': 'Jack', 'age': '24', 'sex': 'male', 'addr': 'no23', 'city':'NY'}

dictofJane = {'name': 'Jane', 'age': '25', 'sex': 'female', 'addr': 'no24', 'city':'NY'}

dictofDane = {'name': 'Dane', 'age': '14', 'sex': 'male', 'addr': 'no14', 'city':'NY'}

In this case, how can I use zip to make it get the dictionaries automatically in a for loop?

falsetru
  • 357,413
  • 63
  • 732
  • 636

1 Answers1

7

Using list comprehension or generator expression:

>>> header = ['name', 'age', 'sex', 'addr', 'city']
>>> values = ['Jack   24   male   no23   NY',
              'Jane   25   female   no24   NY',
              'Dane   14   male   no14   NY']
>>> dictofJack, dictofJane, dictofDane = (
        dict(zip(header, value.split())) for value in values
    )
>>> dictofJack
{'addr': 'no23', 'age': '24', 'city': 'NY', 'name': 'Jack', 'sex': 'male'}
>>> dictofJane
{'addr': 'no24', 'age': '25', 'city':'NY', 'name': 'Jane', 'sex': 'female'}
>>> dictofDane
{'addr': 'no14', 'age': '14', 'city': 'NY', 'name': 'Dane', 'sex': 'male'}

BTW, instead of making multiple variables of dictionaries, I recommend to use dictionary of dictionaries (think of case where 100 of dictionaries required), using dictionary comprehension:

>>> {value.split()[0]: dict(zip(header, value.split())) for value in values}
{'Jane': {'addr': 'no24', 'age': '25', 'city': 'NY', 'name': 'Jane', 'sex': 'female'},
 'Dane': {'addr': 'no14', 'age': '14', 'city': 'NY', 'name': 'Dane', 'sex': 'male'},
 'Jack': {'addr': 'no23', 'age': '24', 'city': 'NY', 'name': 'Jack', 'sex': 'male'}}
Mohammad Yusuf
  • 16,554
  • 10
  • 50
  • 78
falsetru
  • 357,413
  • 63
  • 732
  • 636
  • How about the case when 2 people have the same name? – Thắng Phạm Ngọc Jan 22 '17 at 05:34
  • I have this case: header = ['name', 'age', 'sex', 'addr', 'city'] values = ['Jack 24 male no23 NY', 'Jane 25 female no24 NY', 'Jane 14 male no14 NY'] print {value.split()[0]: dict(zip(header, value.split())) for value in values} It will print like this: {'Jane': {'addr': 'no14', 'age': '14', 'city': 'NY', 'name': 'Jane', 'sex': 'male'}, 'Jack': {'addr': 'no23', 'age': '24', 'city': 'NY', 'name': 'Jack', 'sex': 'male'}} So how can we resolve this? – Thắng Phạm Ngọc Jan 22 '17 at 05:45
  • @ThắngPhạmNgọc, How about using list of dictionaries: `[dict(zip(header, value.split())) for value in values`]`? – falsetru Jan 22 '17 at 06:02
  • it will give back a list of multiple dict like this: [{'addr': 'no23', 'age': '24', 'city': 'NY', 'name': 'Jack', 'sex': 'male'}, {'addr': 'no24', 'age': '25', 'city': 'NY', 'name': 'Jane', 'sex': 'female'}, {'addr': 'no14', 'age': '14', 'city': 'NY', 'name': 'Jane', 'sex': 'male'}] it works, now I will try to split that list into multiple dict. Thank you. – Thắng Phạm Ngọc Jan 22 '17 at 06:18