Create the Winners Dictionary
00:00 To create a dictionary with the keys 1, 2, 3, and the values unknown. We have multiple ways of doing it. I will first show you a very straightforward one.
00:13
Start by declaring the variable named winners
and then curly braces to start a dictionary. And then in the next line, we have the position one.
00:27 That’s the key with the value unknown, and I will copy the string unknown already. So you’re probably seeing what I’m doing here already in the next line, the key two with the value unknown in the next line, the key three, again with the value unknown and I add a comma on the end.
00:49 I would not need to do it here, but it’s good practice to leave it there because if you would add another key-value pair to a dictionary and then you must not forget that there is a comma missing.
01:00 So it’s a good idea to always have the comma there and then close the dictionary with curly braces again.
01:08
Now again, to verify that everything works, you can print()
the winners
dictionary in the next line, and when you save and run the module, then you can see that you now have this dictionary with the keys 1, 2, 3, and the values unknown.
01:27 So that’s one way of doing it. But there is another one that I want to show you, and that’s using a dictionary comprehension, which is basically similar to a list comprehension that you might have seen before.
01:41
I will first write it out and then I will explain it. So you type curly braces and then place: "unknown"
again as a string for place
in range(4)
.
01:54
So you have a range()
function call with an opening parenthesis, and then the four in between. Before I explain it, let’s save and run the module to verify that the winners
dictionary looks like before
02:09
and it almost does except now we have zero as a key in there as well, and that means we need to adjust the range()
function call. But with this output in place, let’s investigate what actually happens here in this dictionary comprehension.
02:28 I mentioned that the dictionary comprehension is similar to a list comprehension. So you have basically a for loop inside the curly braces for the dictionary comprehension, which creates a key and a value pair in every for loop step.
02:42
So you are looping over the range of four. Four is the argument for the range()
function. That means there are four steps. That’s why we have the keys 0, 1, 2, 3, which are four steps, and creating a variable named place
with every step, which then is basically this number of the step.
03:02
We start with a zero, so place
is zero and then one, and then two, and then three.
03:08
Since we are working with a dictionary, you use this place
variable as the key, and then with a colon and the string unknown as the value. So you can basically create a dictionary in one line of code by saying place: "unknown" for place in range(4)
.
03:27
Now, there is this one issue that we start with zero. range()
also has a start parameter. So if you provide the argument start
at the beginning, which in our case should be 1
and then comma and save and run it,
03:46 then you can see that again, you have the dictionary with the keys 1, 2, 3, and the values unknown, just like at the beginning of the lessons when you typed out everything but with one line of code.
03:58 So that’s basically a quite cool way of swiftly creating dictionaries in Python.
04:04
But if you created the dictionary in a different way, no problem, as long as the solution is the same one. And if the solution is the same one, then you can also remove the comments to create the dictionary winners
.
04:17
Yeah, let’s keep the print()
function call with positions in it for now, because I think we will need it. So it’s good to see how positions look, and then print()
the winners
in the end.
04:28
Once you save the module, then you can move on to the next lesson where we need to put the first three positions into the winners
dictionary.
Become a Member to join the conversation.