Follow

Follow
Python enumerate() Function With Example - Beginner's Guide

Python enumerate() Function With Example - Beginner's Guide

Sachin Pal's photo
Sachin Pal
·Mar 20, 2022·

5 min read

Play this article

Table of contents

  • Introduction
  • Python enumerate()
  • Syntax
  • Looping over enumerate() object
  • Performing enumerate() on
  • Conclusion

Introduction

First of all What is the meaning of enumerating?

The meaning of enumerating is to number or name a list of things separately.

Python enumerate() function does the same thing as its name depicts itself.

Python enumerate()

Python enumerate() function takes a collection(for e.g. a list or a tuple) and returns them as an enumerated object.

enumerate() function adds a number/count to each item of the iterable object and returns an enumerate object.

Here's a simple example of enumerate() function usage:

greet = ('Welcome', 'to', 'GeekPython')

output = (enumerate(greet, start=0))

# Converted enumerated object to list
print(list(output))

Output

[(0, 'Welcome'), (1, 'to'), (2, 'GeekPython')]

Syntax

The syntax of enumerate() function is:

enumerate(iterable, start=0)

Parameters

iterable - must be a sequence, an iterator or objects that supports iteration.

start - It is optional. If the start is not specified then the count will start from 0(default) else the count will start from a specified number and increment until the loop ends.

greet = ('Welcome', 'to', 'GeekPython')

output = (enumerate(greet, start=10))

# Converted enumerated object to list
print(list(output))

Output

[(10, 'Welcome'), (11, 'to'), (12, 'GeekPython')]

We specified the start parameter which is equal to 10 and hence the enumerated output starts from the specified number.

Fact

If we see the above example, we converted our enumerate object into the list type and then print it.

What if we don't convert our enumerated object into a list type then what will happen?

Well, the answer is pretty simple we don't get any enumerated object instead, we'll get a location of enumerating object in the memory/CPU.

Example

greet = ('Welcome', 'to', 'GeekPython')

output = (enumerate(greet))
print(output)

Output

<enumerate object at 0x0000016677A25E40>

Looping over enumerate() object

We can perform for loop to iterate over enumerate object.

my_obj = ["Python", "Ruby", "JavaScript", "Java"]

print("Looping over enumerate object:")
for item in enumerate(my_obj):
    print(item)

print("\nLooping over enumerate object using start value:")
for item in enumerate(my_obj, start=5):
    print(item)

Output

Looping over enumerate object:
(0, 'Python')
(1, 'Ruby')
(2, 'JavaScript')
(3, 'Java')

Looping over enumerate object using start value:
(5, 'Python')
(6, 'Ruby')
(7, 'JavaScript')
(8, 'Java')

Performing enumerate() on

We are going to see the examples where we use the enumerate() function on different data types in Python.

String -

Python str type can be iterated so we can perform enumerate() function on it.

Example: Performing enumerate() on string

text = "GeekPython"

# Using loop to iterate and enumerating
print("Using loop to iterate and enumerating")
for word in enumerate(text):
    print(word)

print("----------------xxx--------------------")

# Using simple approach
print("Using simple approach")
output = enumerate(text)
print(list(output))

Output

Using loop to iterate and enumerating
(0, 'G')
(1, 'e')
(2, 'e')
(3, 'k')
(4, 'P')
(5, 'y')
(6, 't')
(7, 'h')
(8, 'o')
(9, 'n')
----------------xxx--------------------
Using simple approach
[(0, 'G'), (1, 'e'), (2, 'e'), (3, 'k'), (4, 'P'), (5, 'y'), (6, 't'), (7, 'h'), (8, 'o'), (9, 'n')]

We can use start parameter also -

text = "GeekPython"

print("Using loop to iterate and enumerating using specified number:")
for word in enumerate(text, start=10):
    print(word)

Output

Using loop to iterate and enumerating using specified number:
(10, 'G')
(11, 'e')
(12, 'e')
(13, 'k')
(14, 'P')
(15, 'y')
(16, 't')
(17, 'h')
(18, 'o')
(19, 'n')

List -

We performed enumerate() function on list type in example below where first block of code given start value as 2 and we used simple approach in the second block of code.

Example: Performing enumerate() on list

greet = ['Welcome', 'to', 'GeekPython']

print("Using enumerate on list using start:")
for iterables in enumerate(greet, start=2):
    print(iterables)

print("\nUsing enumerate on list:")
output = enumerate(greet)
print(list(output))

Output

Using enumerate on list using start:
(2, 'Welcome')
(3, 'to')
(4, 'GeekPython')

Using enumerate on list:
[(0, 'Welcome'), (1, 'to'), (2, 'GeekPython')]

Tuple -

Example: Performing enumerate() on tuple

greet = ('Welcome', 'to', 'GeekPython')

print("Using enumerate on tuple:")
for item in enumerate(greet):
    print(item)

Output

Using enumerate on tuple:
(0, 'Welcome')
(1, 'to')
(2, 'GeekPython')

Dictionary -

dict() type contains key-value pairs, so we can enumerate() dictionary items (keys and values) separately.

Example: Performing enumerate() on dictionary

values = {"a" : "Geek", "b" : "Python"}

print("Accessing values from dict and enumerating:")
for value in enumerate(values.values()):
    print(value)

print("\nAccessing keys from dict and enumerating:")
for key in enumerate(values.keys()):
    print(key)

Output

Accessing values from dict and enumerating:
(0, 'Geek')
(1, 'Python')

Accessing keys from dict and enumerating:
(0, 'a')
(1, 'b')

Example: Performing enumerate() on dictionary using start

values = {"a" : "Geek", "b" : "Python"}

print("Accessing values from dict and enumerating using start:")
for value in enumerate(values.values(), start=5):
    print(value)

print("\nAccessing keys from dict and enumerating using start:")
for key in enumerate(values.keys(), start=7):
    print(key)

Output

Accessing values from dict and enumerating using start:
(5, 'Geek')
(6, 'Python')

Accessing keys from dict and enumerating using start:
(7, 'a')
(8, 'b')

Set -

set() type in Python is unordered, unchangeable, and unindexed, so when we enumerate them, the "set" item appears in random order.

Example: Performing enumerate() on set

greet = {'Welcome', 'to', 'GeekPython'}

print("Using enumerate on set:")
for item in enumerate(greet):
    print(item)

Output

Using enumerate on set:
(0, 'GeekPython')
(1, 'to')
(2, 'Welcome')

Conclusion

As we discussed above, Python enumerate() function helps us to add counter to our iterable objects.

You have seen the implementation of enumerate() function on the built-in data types in Python as well as you've seen how you can loop over iterable objects and enumerate them.

You can use this function in many ways like enumerating the files in the particular directory to make the folder structure pretty or you can make a tool where a user can automatically number their files and folders easily or maybe something else.


That's all for now.

Keep Coding✌✌

Did you find this article valuable?

Support Sachin Pal by becoming a sponsor. Any amount is appreciated!

See recent sponsors | Learn more about Hashnode Sponsors
 
Share this