Convert from list to list of dicts

Question:

I have the following list

a = ['Bananas', 'Ananas', 'Peach', 'Grapes', 'Oranges']

and want to have it as a list of dicts like

b = [{"fruit": "Bananas"},{"fruit": "Ananas"},{"fruit": "Peach"},{"fruit": "Grapes"},{"fruit": "Oranges"}]

How can that be done?

Answers:

You can do:
b = [{'fruit':f} for f in a]

Answered By: John Sloper

These are actually dictionaries not sets inside the list, you can give a try to list comprehension:

a = ['Bananas', 'Ananas', 'Peach', 'Grapes', 'Oranges']
b = [{"fruit": x} for x in a]
print(b)
Answered By: Wasif

If you want to have a key fruit, you should use list comprehension:

b = [{ 'fruit': x } for x in a ]
Answered By: roddar92