What is the Pythonic way to use (elements of) a list as keys in a dictionary?
dictionary, list, python
Solution
you can use dict comprehensions:
In [1]: keys = ['Orange', 'Blue', 'Green']
In [2]: colors={key: [] for key in keys}
In [3]: colors
Out[3]: {'Blue': [], 'Green': [], 'Orange': []}
for python 2.6:
In [4]: colors = dict((key, []) for key in keys)
In [5]: colors
Out[5]: {'Blue': [], 'Green': [], 'Orange': []}
Problem
Given the list: ``` keys = ['Orange','Blue','Green'] ``` and the dictionary ``` colors = {} ``` What is the most Pythonic way to use (the elements of) `keys` as the keys to `colors`? I'm currently doing the following but want to know if there's a better way of using Python than this. ``` for key in keys: colors[key] = [] ``` EDIT: The question originally asked for "the most Pythonic way to use `keys` as the keys to `colors`", but the subsequent code snippet indicates that what's actually required is a way to use its elements.