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

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds 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 the default subtitles language 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 see our video player troubleshooting guide to resolve the issue.

Building my_enum()

00:01 Sometimes it helps to rebuild things to understand them better, so in this lesson, you will rebuild the enumerate() function and call it my_enum().

00:14 So back in VS Code, I use the code command to create a new file. This file I create, and by running it, I create this file anew if it doesn’t exist and save it.

00:33 Of course, you can use your own editor, like I said before.

00:38 And I add the filename on top of it so you know where we are.

00:46 The function you’re creating is also called my_enum(). The function accepts two arguments, so think about what you learned about the enumerate() function.

00:57 The first argument is required. Let’s call it sequence. That’s the iterable that your enumerate() function, my_enum(), will loop through. Also, you learned that there is an optional value called start, and by default it should also be 0, but you could possibly even change this and say, well, my_enum() function should start with 5 because why not?

01:24 But we stick to the zero for now and create a count variable inside of this function that takes over this value. So the default value of start is 0, and inside the function, you initialize a count variable. This should be the value of start.

01:45 And then you run a for loop over the sequence: for item in sequence: yield count, item. And finally, you increment the count variable by 1.

02:06 Oops, I just spotted there is a typo. So let’s check. The argument you’re passing in, sequence, is the one you’re using inside of it, and then you’re looping through it, and you’re yielding count and item. In the end, you’re incrementing count by 1, so every time you’re stepping through this for loop, you count up 1.

02:31 The yielding statement makes this function a generator function. You will learn in the next lesson, by using your my_enum() function, what exactly this does, but for now you have created enough to actually use it.

02:46 So save it, and we’ll see each other in the next lesson.

Become a Member to join the conversation.