Python Forum
Create a dictionary from a list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Create a dictionary from a list
#1
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
Reply
#2
Try
Dict_data = {k: v[0] for k, v in data.items()}
Reply
#3
(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'}}]}
Reply
#4
klllmmm Wrote:my dataset has multiple dictionary elements in the list.
What is the expected value?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Dictionary in a list bashage 2 538 Dec-27-2023, 04:04 PM
Last Post: deanhystad
  filtering a list of dictionary as per given criteria jss 5 665 Dec-23-2023, 08:47 AM
Last Post: Gribouillis
  Sort a list of dictionaries by the only dictionary key Calab 1 487 Oct-27-2023, 03:03 PM
Last Post: buran
  Delete strings from a list to create a new only number list Dvdscot 8 1,510 May-01-2023, 09:06 PM
Last Post: deanhystad
  How to add list to dictionary? Kull_Khan 3 998 Apr-04-2023, 08:35 AM
Last Post: ClaytonMorrison
  [split] why can't i create a list of numbers (ints) with random.randrange() astral_travel 7 1,505 Oct-23-2022, 11:13 PM
Last Post: Pedroski55
  check if element is in a list in a dictionary value ambrozote 4 1,961 May-11-2022, 06:05 PM
Last Post: deanhystad
  Create new dictionary angus1964 6 2,055 May-08-2022, 12:43 PM
Last Post: snippsat
  Create new dictionary angus1964 2 1,253 May-06-2022, 02:14 PM
Last Post: deanhystad
  Dictionary from a list failed, help needed leoahum 7 1,955 Apr-28-2022, 06:59 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020