Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert list of dict to dict

How to convert list of dict to dict. Below is the list of dict

data = [{'name': 'John Doe', 'age': 37, 'sex': 'M'},
        {'name': 'Lisa Simpson', 'age': 17, 'sex': 'F'},
        {'name': 'Bill Clinton', 'age': 57, 'sex': 'M'}]

to

data = {'John Doe': {'name': 'John Doe', 'age': 37, 'sex': 'M'},
        'Lisa Simpson': {'name': 'Lisa Simpson', 'age': 17, 'sex': 'F'},
        'Bill Clinton': {'name': 'Bill Clinton', 'age': 57, 'sex': 'M'}}
like image 465
sush Avatar asked Mar 08 '11 17:03

sush


People also ask

How do you change a list into a dictionary?

Since python dictionary is unordered, the output can be in any order. To convert a list to dictionary, we can use list comprehension and make a key:value pair of consecutive elements. Finally, typecase the list to dict type.

How do I convert a nested list to a dictionary?

We can convert a nested list to a dictionary by using dictionary comprehension. It will iterate through the list. It will take the item at index 0 as key and index 1 as value.

How do you pass a list into a dictionary in Python?

To convert a list to a dictionary using the same values, you can use the dict. fromkeys() method. To convert two lists into one dictionary, you can use the Python zip() function. The dictionary comprehension lets you create a new dictionary based on the values of a list.

How do you convert a list to a key value pair in Python?

For odd items we do the same but starting at index 1 . If we zip those two lists together we obtain a list of (key, value) that we can pass the return value to the dict constructor. Might add a map(str, ....) to the second part to exactly match the desired result.


4 Answers

A possible solution using names as the new keys:

new_dict = {}
for item in data:
   name = item['name']
   new_dict[name] = item

With python 3.x you can also use dict comprehensions for the same approach in a more nice way:

new_dict = {item['name']:item for item in data}

As suggested in a comment by Paul McGuire, if you don't want the name in the inner dict, you can do:

new_dict = {}
for item in data:
   name = item.pop('name')
   new_dict[name] = item
like image 124
joaquin Avatar answered Oct 23 '22 05:10

joaquin


With python 3.3 and above, you can use ChainMap

A ChainMap groups multiple dicts or other mappings together to create a single, updateable view. If no maps are specified, a single empty dictionary is provided so that a new chain always has at least one mapping.

from collections import ChainMap

data = dict(ChainMap(*data))
like image 43
Vlad Bezden Avatar answered Oct 23 '22 05:10

Vlad Bezden


If the dicts wouldnt share key, then you could use:

dict((key,d[key]) for d in data for key in d)

Probably its better in your case to generate a dict with lists as values?

newdict={}
for k,v in [(key,d[key]) for d in data for key in d]:
  if k not in newdict: newdict[k]=[v]
  else: newdict[k].append(v)

This yields:

>>> newdict
`{'age': [37, 17, 57], 'name': ['John Doe', 'Lisa Simpson', 'Bill Clinton'], 'sex': ['M', 'F', 'M']}`
like image 36
phynfo Avatar answered Oct 23 '22 04:10

phynfo


Try this approach:

dict((key, val) for k in data for key, val in k.items())
like image 10
ANK Avatar answered Oct 23 '22 05:10

ANK