FAQ: Creating Dictionaries - List Comprehensions to Dictionaries

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!

1 Like

9 posts were merged into an existing topic: Zip creates an iterator, how can we turn that into a dictionary?

4 posts were split to a new topic: How do dictionaries assign order?

5 posts were split to a new topic: Is this dictionary comprehension realistically used?

A post was split to a new topic: Poor disctiption

If using a zipped list, multiple variables after the for actually makes python automatically access items inside the sub iterable object in the list.

Example:

names_heights_list = list(zip(names, heights))
namesO = [key + "O" for key in names_heights_list]

>>> TypeError: can only concatenate tuple (not "str") to tuple

This wouldn’t work, because here key is accessing the tuples inside names_heights_list.

namesO = [key + "O" for key, value in names_heights_list]

>>> ['JennyO', 'AlexusO', 'SamO', 'GraceO']

This however, does work, because apparently it’s now accessing the items inside the tuples.

Is this some general feature of the langue or is it only applied in this specific case?

It is a feature known as unpacking.

z = (6, 7, 42)

a, b, c = z

print a    # 6
print b    # 7
print c    # 42

If we do not wish to preserve the zip object, but only iterate it the one time then we don’t need to recast it to a list.

Let’s say we wish to generate a dict object of names and heights…

names_heights = dict(zip(names, heights))

or we could have done it with a comprehension…

names_heights = {name: height for name, height in zip(names, heights)}

In your example,

names0 = [f"{name}0" for name, height in zip(names, heights)]

but that seems like the wrong approach if all we want is to suffix the names. We may as well just iterate over the names list.

names0 = [f"{name}0" for name in names]

or we could even use the map iterator…

names0 = map(lambda name: f"{name}0", names)

Ignore the last example if you have not yet taken the unit on iterators, though it must be coming up soon since comprehensions are themselves, simple iterators.

1 Like

It is a feature known as unpacking .

z = (6, 7, 42)
 
a, b, c = z
 
print a    # 6
print b    # 7
print c    # 42

This is cool. I don’t know if I’ve forgot about it, but it seems useful enough to be mentioned somewhere.

Thanks for the detailed explanation! Yeah I haven’t encountered lambda yet, but I’m looking forward to getting to it, since I’ve seen it used in code before but didn’t understand it.

1 Like