Python’s enumerate

enumerate(sequence, start=0)
  • Takes as input a sequence (string, unicode string, list, tuple, bytearray, buffer or xrange) or iterable, and an arbitrary start index
  • Returns an iterator which provides tuples containing the count and value
  • Useful to generate the index corresponding to each element:
for i, animal in enumerate(["cat", "dog", "mouse"], 1):
    print("Animal #{} is a {}".format(i, animal))
PEP 279 — The enumerate() built-in function
enumerate(sequence, start=0)
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s