helpme
Posts: 124
Joined: Thu May 16, 2013 2:20 am

How to combine these 3 lists into a single list of dictionary?

Wed Aug 16, 2017 10:26 am

I have been grappling with this problem for some time. I have 3 python lists.

Code: Select all

feature1_list = [name1, name2, name3]
feature2_list = [1,2,3]
feature3_list = [4,5,6]
I would like to combine these 3 lists into a list of dictionary that looks like this;

Code: Select all

dictionary_list = [{'feature1': 'name1', 'feature2':'1', 'feature3':'4'},{'feature1': 'name2', 'feature2':'2', 'feature3':'5'},{'feature1': 'name3', 'feature2':'3', 'feature3':'6'}]
Any hints or websites on how to get started?

I am using python v3.6

helpme
Posts: 124
Joined: Thu May 16, 2013 2:20 am

Re: How to combine these 3 lists into a single list of dictionary?

Wed Aug 16, 2017 10:49 am

I found the solution with the help of a guru.

Code: Select all

dict_list = [{'feature1': f1, 'feature2': f2, 'feature3': f3} for f1, f2, f3 in zip(feature1_list, feature2_list, feature3_list)]

scotty101
Posts: 3958
Joined: Fri Jun 08, 2012 6:03 pm

Re: How to combine these 3 lists into a single list of dictionary?

Wed Aug 16, 2017 10:53 am

EDIT: We've both done the same thing except you've used a dictionary comprehension which is a bit shorter but often harder to understand.

How does this work for you?

Code: Select all

feature1_list = ['name1', 'name2', 'name3']
feature2_list = [1,2,3]
feature3_list = [4,5,6]

dictionary_list = []
for f1,f2,f3 in zip(feature1_list,feature2_list,feature3_list):
    dictionary_list.append({'feature1': f1, 'feature2': f2, 'feature3': f3})

print(dictionary_list)
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: How to combine these 3 lists into a single list of dictionary?

Wed Aug 16, 2017 10:56 am

EDIT: Gah - by the time I'd managed to type a nice dictionary comprehension you'd found the answer!
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

helpme
Posts: 124
Joined: Thu May 16, 2013 2:20 am

Re: How to combine these 3 lists into a single list of dictionary?

Wed Aug 16, 2017 10:57 am

scotty101 wrote:
Wed Aug 16, 2017 10:53 am
EDIT: We've both done the same thing except you've used a dictionary comprehension which is a bit shorter but often harder to understand.

How does this work for you?

Code: Select all

feature1_list = ['name1', 'name2', 'name3']
feature2_list = [1,2,3]
feature3_list = [4,5,6]

dictionary_list = []
for f1,f2,f3 in zip(feature1_list,feature2_list,feature3_list):
    dictionary_list.append({'feature1': f1, 'feature2': f2, 'feature3': f3})

print(dictionary_list)
Thanks. I like your solution better than mine. It is more readable.

helpme
Posts: 124
Joined: Thu May 16, 2013 2:20 am

Re: How to combine these 3 lists into a single list of dictionary?

Wed Aug 16, 2017 10:58 am

elParaguayo wrote:
Wed Aug 16, 2017 10:56 am
EDIT: Gah - by the time I'd managed to type a nice dictionary comprehension you'd found the answer!
Your solution is most welcome. Your solution may be better than mine. Thanks for the help!

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: How to combine these 3 lists into a single list of dictionary?

Wed Aug 16, 2017 11:07 am

No. Mine was virtually identical (just different variable names)!
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: How to combine these 3 lists into a single list of dictionary?

Wed Aug 16, 2017 11:16 am

Actually, it's not a dictionary comprehension. It's a list comprehension where the members of the list are dictionaries. A dictionary comprehension is different (not that it really matters here!)
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

Return to “Python”