Python Forum

Full Version: Create a dictionary from a list
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have some data in a dictionary but its values are in a list. I want to create a dictionary from these values.

data = {'data':[{'Info':100,'GPSInfo': {1: 'N',2:'S'}}]}
How can I create a dictionary similar to this,

Dict_data = {'data':{'Info':100,'GPSInfo': {1: 'N',2:'S'}}}
Appreciate if someone can help with this
Try
Dict_data = {k: v[0] for k, v in data.items()}
(Oct-06-2019, 04:29 PM)Gribouillis Wrote: [ -> ]Try
Dict_data = {k: v[0] for k, v in data.items()}

Thanks for the answer. It works with the example I have stated above.
However, my dataset has multiple dictionary elements in the list.

data = {'data':[{'Info':100,'GPSInfo': {1: 'N',2:'S'}},{'Info':200,'GPSInfo': {1: 'NN',2:'SS'}}]}
klllmmm Wrote:my dataset has multiple dictionary elements in the list.
What is the expected value?