Leverage dict()
00:00
The first part of the challenge is to create a dictionary captains
, but this time, not an empty dictionary, but based on a list of tuples.
00:11 I have the tuple in my clipboard, so let’s paste them here. So it’s a list with the tuples: Enterprise, Kirk; Voyager, Janeway; and Defiant, Sisko. And now you want to create the dictionary with this list at once.
00:27
And to do this, you could either loop over this list and add them as key-value pairs, or you can make use of the dict()
function. And that’s what I will do here in line three, just right before the square bracket of opening the list.
00:44
I’ll write captains
, that’s the dictionary name equals, and then I call the dict()
function with an opening parenthesis. This function accepts an iterable as an argument, and that’s the list of tuples with the ship names and the captains’ names.
01:03
The important thing here is that you don’t forget the closing parenthesis in line seven after the square bracket. So you add in the full list, and once you save it, you can check if this worked by printing the captains
dictionary in the next line so you can actually see the output.
01:23 And once you run the program, which you can do clicking the menu and click run module or press the F5 key, then you can see that the dictionary, Enterprise, Kirk; Voyager, Janeway; and Defiant, Sisko is present.
01:41
That’s the first part of the challenge, and that’s a good way to start. But before moving on to the next lesson, let’s clean up a little bit and remove the comment from the test that we just did so that we only have the declaration of the captains
dictionary from lines one to five and the print()
call in line seven, and then the comments of the things that we need to do after that.
Become a Member to join the conversation.