Python enumerate(): Simplify Looping With Counters

By realpython - 2020-12-15

Description

Once you learn about for loops in Python, you know that using an index to access items in a sequence isn't very Pythonic. So what do you do when you need that index value? In this tutorial, you'll lea ...

Summary

  • In Python, a for loop is usually written as a loop over an iterable object.
  • >>> >>> print(values[0])a You can see in this example that accessing values with the index 0 gives the first element, a.
  • However, there are many times when you might not want the count from enumerate() to start at 0.
  • On the third line of check_whitespace(), enumerate() is used in a loop over lines.
  • For each elem in sequence, you yield control back to the calling location and send back the current values of n and elem.

 

Topics

  1. Coding (0.28)
  2. UX (0.15)
  3. NLP (0.07)

Similar Articles

Pointers in Python: What's the Point

By realpython - 2021-03-09

In this step-by-step tutorial, you'll get a clearer understanding of Python's object model and learn why pointers don't really exist in Python. You'll also cover ways to simulate pointers in Python wi ...

8 Awesome React Hooks

By DEV Community - 2020-12-30

React.js is currently the most popular JavaScript library for front end developers. Invented by Faceb... Tagged with react, javascript, webdev, beginners.