Python dictionaries (data science - medical insurance project)

Welcome to the Get Help category!

This is where you can ask questions about your code. Some important things to remember when posting in this category :slight_smile:

  • Learn how to ask a good question and get a good answer!
  • Remember to include a link to the exercise you need help with!
  • If someone answers your question, please mark their response as a solution :white_check_mark:
  • Once you understand a new concept, come back and try to help someone else!

Hi. Can anyone explain this to me.
In [Python dictionaries (data science - medical insurance project)] exercise, I tried this.

names = [“Marina”, “Vinay”, “Connie”, “Isaac”, “Valentina”]
ages =[27, 24, 43, 35, 52]
zipped_ages = zip(names, ages)
print(list(zipped_ages))

output: [(‘Marina’, 27), (‘Vinay’, 24), (‘Connie’, 43), (‘Isaac’, 35), (‘Valentina’, 52)]

Then when I entered this:
names_to_ages = {key:value for key, value in zipped_ages}
print(names_to_ages)

output: {}

Can anyone explain why?

In fact, the output of the second method is:

{'Isaac': 35, 'Marina': 27, 'Connie': 43, 'Vinay': 24, 'Valentina': 52}

Note that the key-value pairs can be printed in any order since dictionaries are unordered (meaning they don’t have a specific index position).


Knowing this, could you restate your question? Are you asking why the first method returns a list of tuples and the second returns a dictionary? :slight_smile:

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.