Dict(zip) question

for this exercise

https://app.dataquest.io/m/1011/python-fundamentals-practice-problems/7/converting-a-csv-to-a-dictionary

How would you write the dict(zip(name, email)) code to create the dictionary here?
It was mentioned page 5/13 and looked like an easy alternative, I cant get it to work here though
Thanks

1 Like

using dict() & zip() function here will take a lot of code. The best method that i used was dictionary comprehension

name_to_email={data[0]:data[-1] for data in list(reader(open('users.csv')))[1:]}

Using the dict() & zip() way, you’ll have to extract the columns first, then zip them, then create a dictionary, that will be a long way to go.

1 Like

Okay, thank you for clarifying this

1 Like

Python zip() is an inbuilt function that returns the zip object, which is the iterator of tuples where the first item in each passed iterator is paired together.

From Python 3.6 and beyond, dictionaries are ordered collections, meaning they keep their items in the same order in which they were lead first. If you take leverage of this aspect, then you can use the Python zip() function to iterate over multiple dictionaries safely and coherently.

You can use dictionary comprehension to shorter your code.

1 Like