Must-Know Python Enumerate & How to Master It
Python Enumerate

Must-Know Python Enumerate & How to Master It

Last updated on 28th Jun 2020, Blog, General

About author

Kailash (Sr Technical Project Manager - Python )

He is Highly Experienced in Respective Technical Domain with 11+ Years, Also He is a Respective Technical Trainer for Past 5 Years & Share's This Important Articles For us.

(5.0) | 19623 Ratings 535

The enumerate() function assigns an index to each item in an iterable object that can be used to reference the item later. What does enumerate do in Python? It makes it easier to keep track of the content of an iterable object.

If you are using enumerate in Python 3 and call print(enumerate()), you will get an alphanumeric memory reference address instead of a readable list. enumerate() in Python 3 works this way to save memory by not generating unnecessary full-fledged lists.

Basic syntax of Python enumerate():

To call enumerate() in Python, you will need to provide the function with two arguments.

Example:

  • enumerate(iterable, start=0)

In the example above,

  • iterable is an iterable type object like a list, tuple, or string.
  • start is an optional parameter that tells the function which index to use for the first item of the iterable.

    Subscribe For Free Demo

    Examples how enumerate() can be used:

    If you don’t provide a start parameter, enumerate() will use a start value of 0 by default.

    Example:

    • cars = [‘kia’, ‘audi’, ‘bmw’]
    • for car in enumerate(cars):
    •   print(car)

    However, if you provide a non-zero start value, Python will begin enumerating from it, as demonstrated below:

    Example: 

    • cars = [‘kia’, ‘audi’, ‘bmw’]
    • print(list(enumerate(cars, start = 1)))

    Working with enumerate() in Python:

    The enumerate() function in Python is commonly used instead of the for loop. That’s because enumerate() can iterate over the index of an item, as well as the item itself.

    Using enumerate() also makes the code cleaner, since you have to write fewer lines. For example, recall the code snippet we previously used:

    Example: 

    • cars = [‘kia’, ‘audi’, ‘bmw’]
    • print(list(enumerate(cars, start = 1)))

    Using a for loop to do the same thing as the code above would look something like this,

    Example: 

    Course Curriculum

    Take Your Career to the Next level with Python Training

    • Instructor-led Sessions
    • Real-life Case Studies
    • Assignments
    Explore Curriculum

    • cars = [‘kia’, ‘audi’, ‘bmw’]
    • listOfCars = []
    • n = 0
    • for car in cars:
    •     listOfCars.append((n, car))
    •     n += 1
    • print(listOfCars)

    This code isn’t just longer. You can also see that a complex for loop implementation would get clunky and error-prone easily.

    Working with tuples:

    As mentioned above, you can enumerate over a tuple as well since tuples are iterable objects. Doing so is very similar to enumerating over a list:

    Example:

    • cars = (‘kia’, ‘audi’, ‘bmw’)
    • print(list(enumerate(cars)))

    Working with strings:

    In Python, strings are iterable. That means you can use enumerate() with them the way you would with any other iterable object:

    Example: 

    • str = “HelloWorld”
    • print(list(enumerate(str)))

    Looping through an object with enumerate():

    Let’s say that you have an object that you want to enumerate in Python, and get each component individually. You can loop through the object to do so.

    Python Sample Resumes! Download & Edit, Get Noticed by Top Employers! Download

    S

    • cars = (‘kia’, ‘audi’, ‘bmw’)
    • for car in enumerate(cars):
    • print(car)

    Python enumerate(): useful tips:

    • By default, the Python enumerate() function returns an enumerated object. You can convert this object to a tuple or a list by using tuple(<enumerate>) and list(<enumerate>), respectively.
    • Using enumerate() instead of more verbose loops not only makes the code cleaner but is a more Pythonic way of programming. Such loops can be messy and can have more errors.

    Are you looking training with Right Jobs?

    Contact Us
    Get Training Quote for Free