How do you range enumerate in python?

How to use the enumerate() function in Python

How do you range enumerate in python?

Photo by Florian Olivo on Unsplash

Introduction

Let’s say that we have a list. We want to iterate over this list, printing out the index followed by the list element or value at that index. Let’s accomplish this using a for loop:

num_list= [42, 56, 39, 59, 99]for i in range(len(num_list)): 
print(i, num_list[i])
# output:
0 42
1 56
2 39
3 59
4 99

range() is a built-in function in python that allows us to iterate through a sequence of numbers. As seen above, we use the for loop in order to loop through a range object (which is a type of iterable), up to the length of our list. In other words, we start at an i value of 0, and go up to (but not including) the length of num_list, which is 5. We then access the elements of num_list at the i-th index using square brackets.

However, it is important to understand that we are not actually iterating over num_list. In other words, i serves as a proxy for the index that we can use to access the elements from num_list.

Using the enumerate() Function

Instead of using the range() function, we can instead use the built-in enumerate() function in python. enumerate() allows us to iterate through a sequence but it keeps track of both the index and the element.

enumerate(iterable, start=0)

The enumerate() function takes in an iterable as an argument, such as a list, string, tuple, or dictionary. In addition, it can also take in an optional argument, start, which specifies the number we want the count to start at (the default is 0).

Using the enumerate() function, we can rewrite the for loop as follows:

num_list= [42, 56, 39, 59, 99]for index, element in enumerate(num_list):
print(index, element)
# output:
0 42
1 56
2 39
3 59
4 99

And that’s it! We don’t need to use the range() function. The code looks cleaner and more pythonic.

How enumerate() Works

The enumerate() function returns an enumerate object, which is an iterator. As each element is accessed from this enumerate object, a tuple is returned, containing the index and element at that index: (index, element). Thus, in the above for loop, with each iteration, it is assigning the elements of this returned tuple to the index and element variables. In other words, the returned tuple is being unpacked inside the for-statement:

for index, element in enumerate(num_list):# similar to:
index, element = (index, element)

These tuples can be more easily seen with the following example:

name_list = ['Jane', 'John', 'Mike']list(enumerate(name_list))# [(0, 'Jane'), (1, 'John'), (2, 'Mike')]

Once we call the list() function on the enumerate object (the iterator), it returns a list of tuples, with each tuple including the index and its corresponding element or value.

Conclusion

In this tutorial, we learned how the enumerate() function is used to count through an iterable.

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:

>>>

>>> 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 if condition:

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.

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

What does enumerate () do in python?

Python enumerate() Function The enumerate() function takes a collection (e.g. a tuple) and returns it as an enumerate object. The enumerate() function adds a counter as the key of the enumerate object.

Is enumerate better than range?

enumerate is faster when you want to repeatedly access the list/iterable items at their index. When you just want a list of indices, it is faster to to use len() and range (xrange in Python 2. x).

How do you start an enumerate from 2 in python?

Start index at 1 with enumerate() If you want to start from another number, pass the number to the second argument of enumerate() .

How do you range a string in python?

The most common use of it is to iterate sequence type (Python range() List, string, etc. ) with for and while loop using Python..
range(stop) takes one argument..
range(start, stop) takes two arguments..
range(start, stop, step) takes three arguments..