FAQ: Creating Dictionaries - Review

This community-built FAQ covers the “Review” 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 Review

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!

My solution is below. I found Step 5 really odd in this–it didn’t seem to build on top of steps 1-4 in any practical way, other than just another manipulation of lists. As a result, it took me a solid 10-15 minutes for what is actually a VERY simple step.

My hint for step five is take it very literally, you’re just defining a basic key:value i.e. “The Best Songs” : plays, …

songs = ["Like a Rolling Stone", "Satisfaction", "Imagine", "What's Going On", "Respect", "Good Vibrations"]
playcounts = [78, 29, 44, 21, 89, 5]

plays = {key:value for key, value in zip(songs, playcounts)}

#print(plays)

plays["Purple Haze"] = 1
plays["Respect"] =  94

print(plays)

library = {"The Best Songs": plays, "Sunday Feelings":{} }

print(library)
1 Like

Did anyone else experience this issue: you do exactly what the solution gives, yet it throws errors back at you?

Hello, is there a more convenient way to use operations with dictionaries?
For example going through step 4 in “Review” exercise, intuitively i wanted to write something like
plays["Respect"] += 5
or
plays["Respect"] = plays["Respect"] + 5.
But of course i couldn’t do it.
So is there a way?
Thank you!

Hello @xalava!

plays["Respect"] += 5 or plays["Respect = plays["Respect"] + 5 would work in adding 5 to the value associated with the "Respect" key.

1 Like

Well, not in my case. It would return KeyError: 'Respect' in console area.

Was there a typo or something when initially defining the original key names? Without seeing the previous code it’s hard to say but plays['Respect'] += 5 and similar should work just fine. A key error suggests that particular key is not included in the dictionary. This may be due to a slightly different key name or if the key was never initialised at all (you could only += to an existing key).

2 Likes

Ok, so something i figured.
This is my original code:

songs = ["Like a Rolling Stone", "Satisfaction", "Imagine", "What's Going On", "Respect", "Good Vibrations"]
playcounts = [78, 29, 44, 21, 89, 5]

plays = {key:value for key, value in zip(songs, playcounts)}

print(plays)

plays = {"Purple Haze": 1}

plays["Respect"] = 94

library = {"The Best Songs": plays, "Sunday Feelings": {}}

print()
print(library)

And it outputs:

{'Like a Rolling Stone': 78, 'Satisfaction': 29, 'Imagine': 44, "What's Going On": 21, 'Respect': 89, 'Good Vibrations': 5}

{'The Best Songs': {'Purple Haze': 1, 'Respect': 94}, 'Sunday Feelings': {}}

If i change the line plays["Respect"] = 94 with plays["Respect"] += 5 it will output:

{'Like a Rolling Stone': 78, 'Satisfaction': 29, 'Imagine': 44, "What's Going On": 21, 'Respect': 89, 'Good Vibrations': 5}
Traceback (most recent call last):
  File "script.py", line 10, in <module>
    plays["Respect"] += 5
KeyError: 'Respect'

An Error. But if i also just delete the line plays = {"Purple Haze": 1} it will work just fine again and output:

{'Like a Rolling Stone': 78, 'Satisfaction': 29, 'Imagine': 44, "What's Going On": 21, 'Respect': 89, 'Good Vibrations': 5}

{'The Best Songs': {'Like a Rolling Stone': 78, 'Satisfaction': 29, 'Imagine': 44, "What's Going On": 21, 'Respect': 94, 'Good Vibrations': 5}, 'Sunday Feelings': {}}

Does anyone have any suggestions what can cause it?

Ok it is my bad. The issue was at the beginning. Instead of using plays.update({"Purple Haze": 1}) i’ve made a mistake and just typed plays = {"Purple Haze": 1}.
Sorry for taking space on the forum for no question. If this would not be deleted i hope it will be useful for someone. Thank you anyway.

2 Likes

I had tried to create via the below 2 methods. It threw an error. Can anyone explain why?

library = {}
library[“The Best songs”] = plays
library[“Sunday Feelings”] = {}

and

library = {}
empty = {}
library[“The Best songs”] = plays
library[“Sunday Feelings”] = empty

Is there any difference between these two ways? They both seem to work fine for both adding a pair (key:value) and for updating the value of an already defined key:

plays.update({"Purple Haze":3})

plays["Purple Haze"]=3

(First question here, I hope the format is right)
Thanks!

1 Like

There’s a not a great deal of difference on the surface, they both result in the same thing. Depending on the implementation there are some differences though.

If you're curious possible implementation differences...

For CPython at least, the standard implementation of the Python language, in the first statement you build a new dict object and then pass it as an argument to the .update method, both of these operations take a certain amount of processing to achieve. In the second you’re closely to simply reassigning the key. The second is likely to be ever slightly more efficient in this example (dealing with single keys).

If you wanted to update a dictionary with another large existing dictionary then .update is a much better choice (it can update multiple key-value pairs at once).

Most of the time just pick whatever is more readable :slightly_smiling_face:.

3 Likes

Coming from JS/React, the python spread syntax equivalent ** is how my brain thinks

songs = ["Like a Rolling Stone", "Satisfaction", "Imagine", "What's Going On", "Respect", "Good Vibrations"]

playcounts = [78, 29, 44, 21, 89, 5]

plays = {song:playcount for (song, playcount) in zip(songs, playcounts)}

print(plays)


# set plays equal to the former plays dict and replace the value for "Purple Haze" with '1'
plays = {**plays, "Purple Haze": 1}

# I got a syntax error when I took this further and tried to update the value for Respect
plays = {**plays, "Respect": **plays["Respect"] + 5}

Is there a correct way to get the former value associated the “Respect” key and add to it in this way?

plays = {**plays, "Respect": plays["Respect"] + 5}
plays = {**plays, "Respect": plays["Respect"] + 5}

You don’t need the spread operator on the second instance. It works for the first because you want to bring in the whole dictionary, but the second instance is pulling just the one key:value pair so the spread operator is paradoxical.

EDIT: I don’t know how to format code in this forum hahah

that makes sense, thank you!

1 Like

I guess you could make the dictionary without using a dict comprehension by using the dict constructor function.

plays = dict(zip(songs, playcounts))

or that could be done without using zip by using the indices of the elements from each list:

plays = { songs[i]: playcounts[i] for i in range(len(songs)) }