I have a list of names:
['john smith', 'sally jones', 'bob jones']
I want to build a dict in the following format:
{'john smith': [], 'sally jones': [], 'bob jones': []}
This is what happens when I try using zip
zip((all_crew_names, [[] for item in all_crew_names]))
[(['john smith', 'sally jones', 'bob jones'],), ([[], [], []],)]
What am I doing incorrectly here, and how would I properly zip this up?
The easiest solution here is a dictionary comprehension:
names = ['john smith', 'sally jones', 'bob jones']
d = {name: [] for name in names}
Note that it might be tempting to use dict.fromkeys(names, []), but this would result in the same list being used for all keys.
You don't need zip.
{name: [] for name in all_crew_names}
In older versions of Python there was no such dictionary comprehension, so the following code can be used:
dict((name, []) for name in all_crew_names)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With