FAQ: Creating Dictionaries - List Comprehensions to Dictionaries


#1

This community-built FAQ covers the “List Comprehensions to Dictionaries” exercise from the lesson “Creating Dictionaries”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science

FAQs on the exercise List Comprehensions to Dictionaries

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!


#2

My code:

drinks = ["espresso", "chai", "decaf", "drip"]
caffeine = [64, 40, 0, 120]
zipped_drinks = zip(drinks, caffeine)
print(tuple(zipped_drinks))
drinks_to_caffeine = {drinks:caffeine for drinks, caffeine in zipped_drinks}
print(drinks_to_caffeine)
>>>
(('espresso', 64), ('chai', 40), ('decaf', 0), ('drip', 120))
{}

Why is the dictionary empty when I print it? My response was correct according to the site, but did I not actually make a dictionary?


#3

This prints fine…

drinks = ["espresso", "chai", "decaf", "drip"]
caffeine = [64, 40, 0, 120]
zipped_drinks = zip(drinks, caffeine)
#print(tuple(zipped_drinks))
drinks_to_caffeine = {drinks:caffeine for drinks, caffeine in zipped_drinks}
print(drinks_to_caffeine)

#4

One question here: the combination of key:value is right, but every time I press run, the order of keys is different (espresso, chai, decaf, drip / chai, decaf, drip, espresso / decaf, drip, espresso, chai…), is that ok?


#5

Yes. Order of insertion is not recallable, so not predictable. If order is required then you will need to reach for OrderedDict. That will be for down the road, though as this exercise doesn’t need order.


#6

Do coders use this much? It looks borderline unreadable to me. Mind that I’m a beginner. I just thought python was supposed to readable first in case some else is working off your code in the future. I get that this is new syntax to me, and its seems like we went 0-100 mph, but does list comprehension get used much in real world applications as it seems garbled? I guess what I’m asking is if you could do this without list comprehension AND with the tools/lessons we’ve learned to this point?


#7

Anything that can be done with a comprehension can be done without one. I may be argued that comprehensions are faster but only benchmarking can prove whether this is true in any particular instance.

>>> nums = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
>>> times_two = [x * 2 for x in nums]
>>> doubled = map(lambda x: x * 2, nums)
>>> times_two
[2, 2, 4, 6, 10, 16, 26, 42, 68, 110]
>>> doubled
<map object at 0x02E31030>
>>> list(doubled)
[2, 2, 4, 6, 10, 16, 26, 42, 68, 110]
>>> even_nums = [x for x in nums if not x % 2]
>>> evens = filter(lambda x: not x % 2, nums)
>>> even_nums
[2, 8, 34]
>>> evens
<filter object at 0x02E31130>
>>> list(evens)
[2, 8, 34]
>>> 

Of course we can also write algorithms to perform these tasks. This is the basis for writing comprehensions… The writing of working naive algorithms is what opens the door to other levels of abstraction and sophistication. First we need to master that, then begin working with other tools and refinements. If we skip this step we’re not better off for having better tools.


#8

Thank you very much for getting back to me :slight_smile: I think I see what you’re saying with your examples, but I haven’t had experience with lambda(s) yet. I can see how you’re using list comprehension to manipulate nums, and I’ll just have to keep practicing. I can see where it will click. Thanks again, Happy New Year!


#9

Be sure to write basic algo’s for those two examples to prove to yourself there is a naive solution for each.


#10

Thank you. I’m working on those now. The map() function is very cool !!


#11

I have a similar question as above!
I think the question is that why are we unable to print both the ‘zipped_drinks’ list and the ‘drinks_to_caffeine’ after list comprehension?

image

if I shift both prints to the end of the code, the ‘zipped_drinks’ list appears to be the one that is empty this time round.
Thank you for your time!


#12

Tried it myself and saw the same thing. I did some research and it appears that you can only unpack a zip object once. Once it has been iterated, it is exhausted.

I guess making it into a list when printing it is using the zip object once. Then making it into a dictionary is attempting to use it twice. A site suggested making a separate variable that creates a list of a zip object and I tried that with this lesson and it worked, just a little extra work to see the printed list version of the zip.

This extra work isn’t necessary for the lesson, but helpful to see what your code is doing.

drinks = ["espresso", "chai", "decaf", "drip"]
caffeine = [64, 40, 0, 120]


zipped_drinks = zip(drinks, caffeine)
zipped_drinks_list = list(zip(drinks,caffeine)) #creating a separate variable that is a list version of the zip object

drinks_to_caffeine = {key:value for key, value in zipped_drinks}

print(list(zipped_drinks_list)) #print this new variable so that I don't exhaust the zipped_drinks object
print(drinks_to_caffeine)

If you Google “zip list be unpacked once” you’ll see a couple of helpful more in depth explanations.


#13

There are too many steps here. Using the zip(), you can combine the 2 lists right into a dictionary. Using tuple and zipping more than once isn’t necessary. I am fairly new to python, but not programming, so what I gather Is that this is the fastest, most efficient way. And it works fine!

drinks = ["espresso", "chai", "decaf", "drip"]
caffeine = [64, 40, 0, 120]
drinks_to_caffeine = {drinks:caffeine for drinks, caffeine in zip(drinks, caffeine)}
print(drinks_to_caffeine)

#14

It is only zipped once, but stored in a variable. Abstraction is not something that is easy to teach to a beginner so most lessons make use of intermediary or temporary variables to show that step in a more verbose manner.


#15

Ah, I understand. I haven’t come across tuples yet so I didn’t understand them fully, but soon as I saw the code, I knew I could remove that and the variable and go straight to the dictionary comprehension. Which way is preferred in a real world setting?


#16

I’m no expert on the real world setting and see nothing wrong with your code. In this setting we need to write code that is easy to read and follow, without abstraction in the early stages. Think ‘naive’ over elegant or intuitive. Being a programmer it will seem like a step back, but it can’t hurt to get a refresher and see things from a beginner’s view.


#17

For sure! I sometimes go the naive way when trying to get a function down and then edit for specifics and efficiency later on.