range() vs enumerate()
In this lesson, you’ll learn how to use
enumerate() to solve the classic interview question known as Fizz Buzz.
range() is a built-in function used to iterate through a sequence of numbers. Some common use cases would be to iterate from the numbers 0 to 10:
>>> list(range(11)) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
To learn more, check out The Python range() Function.
enumerate() is a built-in function to iterate through a sequence and keep track of both the index and the number. You can pass in an optional start parameter to indicate which number the index should start at:
>>> list(enumerate([1, 2, 3])) [(0, 1), (1, 2), (2, 3)] >>> list(enumerate([1, 2, 3], start=10)) [(10, 1), (11, 2), (12, 3)]
To learn more, check out Use enumerate() to Keep a Running Index.
Here’s the solution in the video:
for i, num in enumerate(numbers): if num % 3 == 0: numbers[i] = "fizz" if num % 5 == 0: numbers[i] = "buzz" if num % 5 == 0 and num % 3 == 0: numbers[i] = "fizzbuzz"
You could also solve the question using only one
for i, num in enumerate(numbers): if num % 5 == 0 and num % 3 == 0: numbers[i] = "fizzbuzz" elif num % 3 == 0: numbers[i] = "fizz" elif num % 5 == 0: numbers[i] = "buzz"
Both are valid. The first solution looks more similar to the problem description, while the second solution has a slight optimization where you don’t mutate the list potentially three times per iteration.
fizz_buzz() is a function that takes in a list of integers and replaces all integers that are evenly divisible by
3 with the string
"fizz", replaces all integers divisible by
5 with the string
"buzz", and replaces all integers divisible by both
5 with the string
fizz_buzz() on that list, and it mutates that list and replaces the numbers according to these rules. So,
45 is divisible by both
5, so it gets replaced with the string
14 are not divisible by either
So, let’s try to code out the solution using the built-in function
range() takes in a number. Given a list, we have to get the length of the list and pass that in, and it will iterate through the numbers
0 until whatever you pass in.
So, how can we use
range() to solve our problem? Well, we can get that number at that index,
numbers[i], and then we do our checks here.
if num % 3—so, that will check if it’s divisible, because
% (modulo) gets the remainder and if there is no remainder, then it is divisible by
numbers. Cool! That output looks correct. Before we move on, let’s just quickly go over the
doctest module in Python. So, it’s a really nice module that we will actually go over more in detail in a couple videos, but basically when you run
python3 -m doctest and then the filename, it will look at all your functions, look at the docstring, look at the doctests, and then run them and compare the output here with the output of your code. So when we run it, if nothing is outputted, that means it passes all the tests. But if—let’s just say—we change this string, we run it, it says
Expected—this is our expected—but we actually got this by running the code.
So now, let’s clean this code up using
enumerate() is a function that takes in an iterable and iterates through that iterable and returns tuples where the first element of the tuple is the index and the second element is the value.
So, if we do something like
[tup for tup in enumerate([1, 2, 3])]—forgot to close—we get
0—which is the index,
1—which is the value,
1—which is the index,
2—which is the value. So, how can we use that to clean this up?
So, the code will look like this. And our index is
12. So, that concludes the video regarding
enumerate(). In the next video, you will learn about list comprehensions and some useful list methods.
Become a Member to join the conversation.