Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Loop Over the Positions

00:00 To fill up the winner’s dictionary with the positions, there are two strategies that come to my mind. So basically we have the positions list and the winner’s dictionary.

00:10 The question I’m having right now is should we loop over the winner’s dictionary and then pick the positions as the values, or should we loop over the positions list and then add the positions to the winner’s dictionary?

00:25 Thinking about it, I would rather loop over the positions list and add it to the winner’s dictionary because generally it’s not such a good idea to loop over something and change it at the same time.

00:39 If you would loop over the winner’s dictionary, then we would change the values with every step while looping over a dictionary, and you can run into some weird bugs if you do it like that.

00:51 So it’s a better idea to loop over the positions and then add them to the winner’s dictionary because basically we just need the contents of the positions list and don’t change anything with the positions list, but we change something with winners.

01:06 That’s my strategy. Let’s do that.

01:11 Let’s start slow by just looping over the positions list without doing anything fancy for now. For spaceship in positions,

01:24 I want to print the spaceship.

01:29 Let’s save and run the module,

01:34 and then you can see that it prints all besi, enterprise, defiant, and Voyager. If you look a little bit further up, you see that that’s basically the list that you’re printing out in line 11, where you print out the full list at once.

01:49 That’s good. That means so far everything is working as expected.

01:55 Next, I want to make use of Python’s enumerate() function because I also want to have the index of this list item while I’m looping over this list.

02:07 What you can do in Python is that you create a for loop and define a tuple named, for example, index , and then spaceship in I will explain this in a moment, a little bit further in enumerate(), and then let’s just put positions in there for now as an argument and see what comes out when we are printing in the next line.

02:36 Again, the index variable and spaceship variable, and once you’ve saved and run the module, I will explain a bit more what happens under the hood.

02:47 Maybe you remembered from one of the former lessons working with the items() dictionary method and using enumerate() kind of is a little bit similar when you are working with lists.

02:59 In the for loop definition, you can define two variables, and again, you can name them any way you want, but the first part of the tuple is kind of like an index, so it’s usually a good idea to name this variable index or number or step or something like that.

03:17 And the second part of the tuple is the item of the list. In our case, that’s the spaceship name. That’s why I call the variable here, spaceship.

03:27 And then you can call the enumerate() function and pass in an iterable. In our case, it’s the positions list, so that’s why we get 0 enterprise, 1 voyager, 2 alt besi, and 3 defiant in our print output, which is going into the direction that we want, but not entirely.

03:49 So let’s dive a little bit deeper in the next lesson.

Become a Member to join the conversation.