You've read a lot about functions, and it's about time that you practice writing them. In this mini-project, your task is to re-create a built-in function that comes with Python.
You'll get to know the enumerate() function and then venture off to create the function from scratch by yourself.
Python's Built-In Enumerate
Like some of the functions you've revisited earlier in this section, enumerate() is a built-in function that comes with the Python language. It's mostly used to keep an additional count when iterating over a collection using a for loop:
courses = ['Intro', 'Intermediate', 'Advanced', 'Professional']
for index, course in enumerate(courses):
print(f"{index}: {course} Python")
# OUTPUT:
# 0: Intro Python
# 1: Intermediate Python
# 2: Advanced Python
# 3: Professional Python
In the code snippet above, you can see enumerate() in action. You're first defining a list called courses that holds a couple of string values.
Then, you create a for loop that is a bit different than it would be if you were looping directly over the list. Instead of defining only one loop variable, you're working with two of them, index and course.
You have access to two loop variables because of what enumerate() does to your courses list. Instead of providing only the current item in your iterator, enumerate() packages each item together with an incrementing number into a tuple:
(0, 'Intro')
(1, 'Intermediate')
(2, 'Advanced')
(3, 'Professional')
Your for loop then implicitly uses tuple unpacking to take the tuples generated by enumerate() apart again and provide them as two separate variables that you descriptively named index and course.
Using enumerate() to keep track of the counts in your for loops is a common way to tackle this task in Python.
Alternatives
As usual in programming, there are many different ways to solve a challenge. Here's another way to keep a count during a for loop based on a different structure that is common in other programming languages:
courses = ['Intro', 'Intermediate', 'Advanced', 'Professional']
for index in range(len(courses)):
print(f"{index}: {courses[index]} Python")
Here, you're using a familiar for loop with only one loop variable, together with two other built-in functions, range() and len(), to access an index count of the list items. You're then using list indexing in the loop body to access the values of the courses list.
As you can see, there's more than one way to solve this challenge, but Python's enumerate() is a well-liked function in the Pythonsphere. So why not get to know it more closely?
Your Custom Enumerate
This project will train you in your understanding of data types, writing functions, and reading documentation.
Your task is to create a function called my_enumerate() that mirrors the functionality of Python's built-in enumerate().
Don't just copy the range(len(sequence)) example and work off of it. Instead, go online, train your search engine skills, and research how enumerate() works internally. Then, use this to inform your design decisions for your custom my_enumerate() function.
Ultimately, you should be able to use my_enumerate() as a drop-in replacement for enumerate() that can be used in the same way and will produce the same results:
def my_enumerate():
# You implement this function
pass
courses = ['Intro', 'Intermediate', 'Advanced', 'Professional']
for index, course in my_enumerate(courses):
print(f"{index}: {course} Python")
# OUTPUT:
# 0: Intro Python
# 1: Intermediate Python
# 2: Advanced Python
# 3: Professional Python
Take your time to understand the task at hand. Do your research to understand how enumerate() works. Write all the pseudocode you need. Ask questions in Discord. And start building your own pythonic function.