# range() vs enumerate()

In this lesson, you’ll learn how to use `range()` and `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:

Python
``````>>> list(range(11))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
``````
Copied!

`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:

Python
``````>>> list(enumerate([1, 2, 3]))
[(0, 1), (1, 2), (2, 3)]
>>> list(enumerate([1, 2, 3], start=10))
[(10, 1), (11, 2), (12, 3)]
``````
Copied!

To learn more, check out Use enumerate() to Keep a Running Index.

Here’s the solution in the video:

Python
``````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"
``````
Copied!

You could also solve the question using only one `if` condition:

Python
``````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"
``````
Copied!

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.

In the video, you saw the interactive Python Terminal iPython. It has color coating and many useful built-in methods. You can install it. James Uejio RP Team

I use the interactive Python Terminal iPython. It has color coating and many useful built in methods. You can install it here. James DeHart

Mind blown. Thank you. Abu Shoeb

Hi James, I installed iPython and ran it via command line but I don’t see similar interface like your have. Can you please tell me how I can have the similar setup as you? Thanks James Uejio RP Team

Hi @Abu I am also using VSCode in the video with “Dainty – Monokai” theme. I have a split screen setup with iPython + Terminal on one side and code on the other side. Abu Shoeb

I actually realized it after posting the comment :-) Thanks a lot though. Agil C

Hey @James Thanks for the video and mentioning ipython. I didn’t knew about ipython before, so I tried it after watching the view. But when i get into ipython it was surprising the interface was very familiar. I was using it daily for the past couple o months in django shell, didn’t noticed I was using ipython. LOL. Mark R Baker

Hello James, great material! My question is does the order of the if statements matter? I had thought that the if statement for those numbers divisible by 5 and 3 should come first, because it is the strictest. I am a newb, so I apologize if this is a dumb question. Thanks, Mark James Uejio RP Team

@Agil Yea IPython creeps into everything! I just learned about the `ipdb` debugger which expands on pdb. `import ipdb; ipdb.set_trace()` pypi.org/project/ipdb/

@Mark No dumb questions at all! You are right there is another way to write the code:

``````if num % 5 == 0 and num % 3 ==0:
numbers[i] = "fizzbuzz"
elif num % 5 == 0:
numbers[i] = "buzz"
elif num % 3 == 0:
numbers[i] = "fizz"
``````

But be careful you need to use if-elif-elif or else the following conditions will override the first one (3 if statements). Try it out yourself! Mark R Baker

Thanks, James! SatyaRavipati

One More version

``````"""
numbers = [45, 22, 14, 65, 97, 72]
Replace all integers divisible by 3 with "fizz"
Replace all integers divisible by 5 with "buzz"
Replace all integers divisible by 3 and 5 with "fizzbuzz"
>>> my_list_1 = [45, 22, 14, 65, 97, 72]
>>> my_list_2 = [45, 22, 14, 65, 97, 72]
>>> use_range(my_list_1)
Values: ['fizzbuzz', 22, 14, 'buzz', 97, 'fizz']
>>> use_enumerate(my_list_2)
Values: ['fizzbuzz', 22, 14, 'buzz', 97, 'fizz']
"""
from functools import lru_cache

@lru_cache
def is_true_div_by(num, div_num):
val = True if num % div_num == 0 else False
return val

def use_range(my_list: list) -> None:
for idx in range(len(my_list)):
num = my_list[idx]
if is_true_div_by(num, 3) and is_true_div_by(num, 5):
my_list[idx] = "fizzbuzz"
elif is_true_div_by(num, 3):
my_list[idx] = "fizz"
elif is_true_div_by(num, 5):
my_list[idx] = "buzz"
print(f"Values: {my_list}")

def use_enumerate(my_list: list) -> None:
for idx, num in enumerate(my_list):
if is_true_div_by(num, 3) and is_true_div_by(num, 5):
my_list[idx] = "fizzbuzz"
elif is_true_div_by(num, 3):
my_list[idx] = "fizz"
elif is_true_div_by(num, 5):
my_list[idx] = "buzz"
print(f"Values: {my_list}")
`````` dddiiirrr3

You wrote this:

``````if num % 3 == 0 and num % 5 == 0:
``````

We can rewrite this line like this:

``````if num % 15 == 0:
``````

If the number should be divisible by 3 and by 5, then it should be divisible by 3 * 5 = 15.

I think it looks more concise. :)

to join the conversation.

Lesson Completed!
Lesson Bookmarked
Request Failed :(