1. Home
  2. Docs
  3. Python 201
  4. Enumerate in Python

Enumerate in Python

In this lesson you will learn about, enumerate built in function.

Suppose we have list of items,

 

icecream_flavors = ['chocoloate','butterscotch','dark current','vanilla']

  
And want to display them to user.

We can do that by iterating through the list and print.
 
 

icecream_flavors = ['chocolate','butterscotch','dark current','vanilla']

for flav in icecream_flavors:

    print(flav)


output:

chocolate

butterscotch

dark current

vanilla

  
This is fine, but what if you want to show, serial numbers against each item.

We can also do that by using for-loop and range() function, like below.
  

for i in range(len(icecream_flavors)):

    print('%d : %s ' % (i+1,icecream_flavors[i]))


output:

1 : chocolate 

2 : butterscotch 

3 : dark current 

4 : vanilla 

  
Nice output right,

In above example list length is passed as an argument to range(),

range([start],end,[step]) is a built in function.

Note we are providing only ‘end’ argument as len(icecream_flavors).

and we have ‘i’ as index, which starts from zero till len(icecream_flavors) -1.

enumerate() function

Is there a elegant way of doing it ?

Yes, Python provides enumerator() built-in function. By using this

we can get the same output as above.

Let’s see an example.

 
syntax:
  

enumerate(iterable[, start])

  
enumerate takes an iterable object, as a mandatory argument,

start is optional, zero if not provided.

iterable object can be a list, string,

tuple or any object which support iteration etc..

enumerator() returns an generator object, which yields a pair of count and value.

 

Let’s use enumerate,

 

enumerate(icecream_flavors)

ouput:

<enumerate at 0x1b0c39eb318>


enumerate(icecream_flavors) returns an enumerate object at 0x1b0c39eb318.

  

How to access the values form enumerate object?

We have two ways of accessing the values,

Method 1:
 
Assign this enumerate object to a variable

and access values by using next() function.

 
 

en = enumerate(icecream_flavors)

#access element using next() built-in function.

next(en)

output:

(0, 'chocoloate')

#access next element

next(en)

output:

(1, 'butterscotch')

  

Method 2:

 
Accessing all elements through enumerator using a for-loop.
  

for i,flav in enumerate(icecream_flavors):

    print('%d : %s ' % (i,flav))

output:

0 : chocolate 

1 : butterscotch 

2 : dark current 

3 : vanilla 

  

Did you notice ? series start from zero,

It’s because, we have passed only one argument to enumerate() function,

i.e icecream_flavors which is an iterable object.

Let’s pass the start argument, to enumerate,
  

for i,flav in enumerate(icecream_flavors,1):

    print('%d : %s ' % (i,flav))


output:

1 : chocoloate 

2 : butterscotch 

3 : dark current 

4 : vanilla

  

Now the output looks fine, right?

You can pass any number to enumerate,
  

enumerate(icecream_flavors,10)

or even negative numbers.

  

enumerate(icecream_flavors,-10)

Let’s look at some more example of enumerator.

 
Ex 1:
 
Parse a given string and get index of each word.
  

def index_words(text):

    result = []

    if text:

        result.append(0)

    for index, letter in enumerate(text,1):

        if letter == ' ':

            result.append(index)

    return result

address = "There's a lady who's sure All that glitters is gold"

index_words = index_words(address)

print(index_words)


outut:

[0, 8, 10, 15, 21, 26, 30, 35, 44, 47]

  

Let’s break down the example to understand the flow.

In the above example, we have an empty list ‘result’,

at first we append ‘0’ which is the starting index of first word in ‘text’,

next we check for each letter in string, if letter is not a space,

we continue iteration, if there is space, which means its start of new word,

that index is added to ‘result’ list.