List Comprehension to Dictionary Question

Here’s the project I’m working on:

Here’s the code I wrote that’s puzzling me:

names = ['Cuba I', 'San Felipe II Okeechobee', 'Bahamas', 'Cuba II',  
'CubaBrownsville', 'Tampico', 'Labor Day', 'New England', 'Carol', 'Janet', 'Carla',
 'Hattie', 'Beulah', 'Camille', 'Edith', 'Anita', 'David', 'Allen', 'Gilbert',
 'Hugo', 'Andrew', 'Mitch', 'Isabel', 'Ivan', 'Emily', 'Katrina', 'Rita', 'Wilma',
 'Dean', 'Felix', 'Matthew', 'Irma', 'Maria', 'Michael']

lst = ['Name' for i in range(len(names))]
zipped = list(zip(lst, names))
name_d = {word:name for word, name in zipped}

The output is:

{'Name': 'Michael'}

This is more like a general question but it’s related to the project. Anyways, I just find it odd that when I print out the dictionary, it only shows one element. Why is this?

Thanks in advance!

1 Like


Dictionaries can only have unique keys. With this command: name_d = {word:name for word, name in zipped} you’re mapping the names to ‘Name’ as a key. But again, ‘Name’ can only be a key once in a dictionary.


Well I feel extremely dumb. Totally forgot about that. Thanks for the help!

1 Like

Not dumb at all, it’s easy to forget! :sweat_smile: (I forgot recently when using sets)