Laura Smith
Laura Smith

Reputation: 313

How to create a dictionary of items in a specific format?

I have a list in python called itemList which contains data as the following:

itemList = [Soil, Temperature, Humidity,...]

I want to store this information from the list into a dictionary in the following format:

{
   "Characteristic": "Soil",
   "Info": "ToRead"
},
{
   "Characteristic": "Temperature",
   "Info": "ToRead"
},
...

The "Info" field is fixed in all of the {}. I was not sure on how to create this structure. Any help would be appreciated.

Thank you.

Upvotes: 0

Views: 76

Answers (6)

tobias_k
tobias_k

Reputation: 82889

You can use a list-comprehension with nested dictionary literals to create the data...

>>> itemList = ["Soil", "Temperature", "Humidity"]                          
>>> d = [{"Characteristic": c, "Info": "ToRead"} for c in itemList]        
>>> d                                                                      
[{'Characteristic': 'Soil', 'Info': 'ToRead'},
 {'Characteristic': 'Temperature', 'Info': 'ToRead'},
 {'Characteristic': 'Humidity', 'Info': 'ToRead'}]

... then use json.dump or dumps to dump it to a file or pretty-print it.

>>> import json                                                            
>>> print(json.dumps(d, indent=2))                                         
[
  {
    "Characteristic": "Soil",
    "Info": "ToRead"
  },
  {
    "Characteristic": "Temperature",
    "Info": "ToRead"
  },
  {
    "Characteristic": "Humidity",
    "Info": "ToRead"
  }
]

Upvotes: 0

Philippa Richter
Philippa Richter

Reputation: 53

You could try something like this:

dicts = []
for item in itemList[:]:
    newDict = {"Item:item, "Info":"toRead"}
    dicts.append(newDict)

I hope this helps, and that I didn't misinterpret the problem! I'm assuming the format you presented was a list of dictionaries.

Upvotes: 0

K.Lesniak
K.Lesniak

Reputation: 27

itemList = ['Soil', 'Temperature', 'Humidity']

data = []


for item in itemList:
    data.append({
        "Characteristic": item,
        "Info": "ToRead"
    })

print(str(data))

Upvotes: 0

niko
niko

Reputation: 360

If you want to create a list of dicts it's easily done with a for loop.

itemList = ['Soil', 'Temperature', 'Humidity']
output = []
for item in itemList:
    output.append({'Characteristic': item, 'Info': 'ToRead'})

Now output should contain a list of dicts:

[{'Characteristic': 'Soil', 'Info': 'ToRead'}, {'Characteristic': 'Temperature', 'Info': 'ToRead'}, {'Characteristic': 'Humidity', 'Info': 'ToRead'}]

Upvotes: 1

Adirio
Adirio

Reputation: 5286

Short answer: dictList = [{"Characteristic": item, "Info": "ToRead"} for item in itemList]

A list comprehension that iterates over the item list and creates the desired dict for each item. Simple, one-liner, pythonic, explicit, not much more to say about it.

Upvotes: 1

Rakesh
Rakesh

Reputation: 82755

Use dict inside a list comprehension

Ex:

itemList = ["Soil", "Temperature", "Humidity"]
result = [dict(Characteristic=i, Info="ToRead") for i in itemList]
print(result)

Output:

[{'Characteristic': 'Soil', 'Info': 'ToRead'},
 {'Characteristic': 'Temperature', 'Info': 'ToRead'},
 {'Characteristic': 'Humidity', 'Info': 'ToRead'}]

Upvotes: 1

Related Questions