Generating a dictionary with value zeroed and keys from list of lists

I was wondering about the idiomatic way Python to initialize a dictionary with all values set to zero and the keys coming from a list. A little research gave me the idea that dict.fromkeys was a common way and certainly seems pretty succinct.

colors = ['red', 'green', 'blue']
color_scores = dict.fromkeys(colors, 0)
# {'red': 0, 'green': 0, 'blue': 0}

I was also wondering about the idiomatic to obtain a list from a lists of lists i.e. given:

fruits = [['fruit_name', 'color', 'rating'], ['jonathan apple', 'red', 63], ['gala apple', 'green', 33], ['blueberry', 'blue', 99] ]

generate

['jonathan apple', 'gala apple', 'blueberry']

or combing the dictionary initialization, generate
{'jonathan apple': 0, 'gala apple': 0, 'blueberry': 0}

This following style sat well with popular approaches from other languages but I did not know if this was very idiomatic for Python, especially when used as nested expression

fruit_names = list(map( lambda row : row[0], fruits[1:]))
fruit_scores = dict.fromkeys(list(map( lambda row : row[0], fruits[1:])), 0)

The latter line would output what we were looking for:
{'jonathan apple': 0, 'gala apple': 0, 'blueberry': 0}

I had the impression that list comprehensions was a more idiomatic in Python for list transformation, and it looks pretty good for this example below

fruit_scores = dict.fromkeys([row[0] for row in fruits[1:]], 0)

Any feedback appreciated.

1 Like

I think others might be able to help you better if you added a bit more detail on your actual input and expected output.

Lists don’t have columns. List of lists have rows, so you could consider them having columns too and I believe that’s what you might be referring to. But an example of the list of lists would be helpful.

And a sample of the expected output based on that list of lists would also be helpful.

Maybe then someone can try to suggest alternative ways to do this.

Thanks for calling the poor wording on my question to attention as it did seem to be quite unclear when I reviewed it. I reworded this to focus on Python style for dictionary and list manipulation.

Thanks for rewording it.

Since you are already aware of list comprehensions, I think the best possible way might be to use Dictionary Comprehension for this.

Something as simple as -

{val[0]:0 for val in fruits[1:]}

The above will yield the same result and for significantly large inputs, it is likely to be faster than your non-list comprehension approach.

Your list comprehension approach is comparable to the dictionary comprehension one. But the latter is more “pythonic” in this particular scenario.

3 Likes

Thanks, that’s what I was looking for

I am just starting to learn the power of list comprehensions; they are so awesome! I never thought about dictionary comprehensions but it totally makes sense…I wonder where else we could apply such a “coding strategy?”

Also, @radlab773 would you kindly mark the_doctor’s answer above as being the solution, please & thank you!

1 Like