dana the 8 year old anorexic

dana the 8 year old anorexic


Suggest a Topic
Write an Article

  • zip() in Python
  • Python String
  • Python Language Introduction
  • Python List
  • Find the first non-repeating character from a stream of characters
  • Python 3 basics
  • Array in Python | Set 1 (Introduction and Functions)
  • Python Dictionary
  • How to print without newline in Python?
  • Python | Set 2 (Variables, Expressions, Conditions and Functions)
  • Python String | split()
  • Python map() function
  • Object Oriented Programming in Python | Set 1 (Class, Object and Members)
  • Keywords in Python | Set 1
  • Reverse string in Python (5 different ways)
  • Heap queue (or heapq) in Python
  • Linear Regression (Python Implementation)
  • Python GUI – tkinter
  • Global and Local Variables in Python
  • How to split a string in C/C++, Python and Java?
  • Important differences between Python 2.x and Python 3.x with examples
  • Twitter Sentiment Analysis using Python
  • How to input multiple values from user in one line in Python?
  • NumPy in Python | Set 1 (Introduction)
  • Python Tuples
  • How to check if a string is a valid keyword in Python?
  • Permutation and Combination in Python
  • Function Decorators in Python | Set 1 (Introduction)
  • join() function in Python

Enumerate() in Python

A lot of times when dealing with iterators, we also get a need to keep a count of iterations. Python eases the programmers’ task by providing a built-in function enumerate() for this task.
Enumerate() method adds a counter to an iterable and returns it in a form of enumerate object. This enumerate object can then be used directly in for loops or be converted into a list of tuples using list() method.


enumerate(iterable, start=0)Parameters:Iterable: any object that supports iterationStart: the index value from which the counter is to be started, by default it is 0 




# Python program to illustrate
# enumerate function
l1 = ["eat","sleep","repeat"]
s1 = "geek"
# creating enumerate objects
obj1 = enumerate(l1)
obj2 = enumerate(s1)
print "Return type:",type(obj1)
print list(enumerate(l1))
# changing start index to 2 from 0
print list(enumerate(s1,2))



Return type: < type 'enumerate' >[(0, 'eat'), (1, 'sleep'), (2, 'repeat')]
[(2, 'g'), (3, 'e'), (4, 'e'), (5, 'k')]

Using Enumerate object in loops





# Python program to illustrate
# enumerate function in loops
l1 = ["eat","sleep","repeat"]
# printing the tuples in object directly
for ele in enumerate(l1):
    print ele
# changing index and printing separately
for count,ele in enumerate(l1,100):
    print count,ele



(0, 'eat')
(1, 'sleep')
(2, 'repeat')
100 eat
101 sleep
102 repeat

This article is contributed by Harshit Agrawal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to [email protected] See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

My Personal Notes

Recommended Posts:

  • Important differences between Python 2.x and Python 3.x with examples
  • Python | Set 4 (Dictionary, Keywords in Python)
  • Python | Sort Python Dictionaries by Key or Value
  • Python | a += b is not always a = a + b
  • bin() in Python
  • Any & All in Python
  • chr() in Python
  • SQL using Python | Set 1
  • max() and min() in Python
  • SHA in Python
  • set add() in python
  • abs() in Python
  • Python Set | pop()
  • pow() in Python
  • try and except in Python
Improved By : Rounak_agarwal









Writing code in comment? Please use ide.geeksforgeeks.org , generate link and share the link here.

Most popular in Python
  • class method vs static method in Python
  • range() vs xrange() in Python
  • Keywords in Python | Set 2
  • Basic calculator program using Python
  • Python lambda (Anonymous Functions) | filter, map, reduce

More related articles in Python
  • Type Conversion in Python
  • Sets in Python
  • Python Input Methods for Competitive Programming
  • When to use yield instead of return in Python?
  • Class or Static Variables in Python

Most visited in Python
  • Python Sets
  • Naive Bayes Classifiers
  • Count words in a given string
  • Understanding Logistic Regression
  • K means Clustering – Introduction
  • *args and **kwargs in Python
  • Python program to check if a string is palindrome or not
  • loops in python
  • Socket Programming in Python
  • Decision Making in Python (if , if..else, Nested if, if-elif)
  • Python3 Intermediate Level Topics
  • Programs for printing pyramid patterns in Python
  • isupper(), islower(), lower(), upper() in Python and their applications
  • Python String | find()
  • Loops and Control Statements (continue, break and pass) in Python

Stack Overflow

  1. Log In
    Sign Up

  2. current community

    • Stack Overflow


    • Meta Stack Overflow

    your communities

    Sign up or log in to customize your list.

    more stack exchange communities

    company blog

    • Tour

      Start here for a quick overview of the site

    • Help Center

      Detailed answers to any questions you might have

    • Meta

      Discuss the workings and policies of this site

    • About Us

      Learn more about Stack Overflow the company

    • Business

      Learn more about hiring developers or posting ads with us

By using our site, you acknowledge that you have read and understand our Cookie Policy , Privacy Policy , and our Terms of Service .

What does enumerate mean?

Ask Question

up vote
down vote



I am using tkinter in Python and came across the following code:

for row_number, row in enumerate(cursor):

I was wondering whether anyone could explain what enumerate means in this context?

python enumerate

share | improve this question

edited Aug 9 ’16 at 9:23

Martijn Pieters


asked Mar 4 ’14 at 12:08



  • 14

    –  ndpu
    Mar 4 ’14 at 12:10

add a comment  | 

5 Answers




up vote
down vote

The enumerate() function adds a counter to an iterable.

So for each element in cursor, a tuple is produced with (counter, element); the for loop binds that to row_number and row, respectively.


>>> elements = ('foo', 'bar', 'baz')
>>> for elem in elements:
... print elem
>>> for count, elem in enumerate(elements):
... print count, elem
0 foo
1 bar
2 baz

By default, enumerate() starts counting at 0 but if you give it a second integer argument, it’ll start from that number instead:

>>> for count, elem in enumerate(elements, 42):
... print count, elem
42 foo
43 bar
44 baz

If you were to re-implement enumerate() in Python, here are two ways of achieving that; one using itertools.count() to do the counting, the other manually counting in a generator function :

from itertools import count
def enumerate(it, start=0): # return an iterator that adds a counter to each element of it return zip(count(start), it)


def enumerate(it, start=0): count = start for elem in it: yield (count, elem) count += 1

The actual implementation in C is closer to the latter, with optimisations to reuse a single tuple object for the common for i, ... unpacking case and using a standard C integer value for the counter until the counter becomes too large to avoid using a Python integer object (which is unbounded).

share | improve this answer

edited Jul 12 at 11:45

answered Mar 4 ’14 at 12:10

Martijn Pieters


  • Your demo is useful, but could you explain condition also as [ i for i,x in enumerate(wl),if x == "gonna"]
    –  Hemant Yadav
    May 28 ’17 at 16:53

  • 2

    @HemantYadav: that’s just part of a list comprehension (albeit with an error in it, that second comma should probably not be there), and is not specific to enumerate(). See Python for-in loop preceded by a variable ; it builds a list of indices for all occurrences of the string 'gonna' in wl.
    –  Martijn Pieters
    May 28 ’17 at 17:17

add a comment  | 

up vote
down vote

It’s a builtin generator function, see http://docs.python.org/2/library/functions.html#enumerate .

In short, it yields the elements of an iterator, as well as an index number:

for item in enumerate(["a", "b", "c"]): print item


(0, "a")
(1, "b")
(2, "c")

It’s helpful if you want to loop over an interator, and also want to have an index counter available. If you want the counter to start from some other value (usually 1), you can give that as second argument to enumerate.

share | improve this answer

edited Mar 4 ’14 at 12:31

answered Mar 4 ’14 at 12:12



  • 1

    I think you’re missing a ] in your first line of code.
    –  elParaguayo
    Mar 4 ’14 at 12:29

  • 1

    if you add a second variable to item then you can remove the parenthesis 😀
    –  Abdelouahab
    Dec 14 ’14 at 0:19

add a comment  | 

up vote
down vote

I am reading a book (‘Effective Python’) by Brett Slatkin and he shows another way to iterate over a list and also know the index of the current item in the list.
BUT suggests to not use it and use enumerate instead.
I know you asked what enumerate means, but when I understood the following, I also understood how enumerate makes iterating over a list while knowing the index of the current item easier (and more readable).

list_of_letters = ['a', 'b', 'c']
for i in range(len(list_of_letters)): letter = list_of_letters[i] print (i, letter)

The output is:

0 a
1 b
2 c

I also used to do something, even sillier before I read about the enumerate function.

i = 0
for n in list_of_letters: print (i, n) i = i +1

It produces the same output.

But with enumerate I just have to write:

list_of_letters = ['a', 'b', 'c']
for i, letter in enumerate(list_of_letters): print (i, letter)

share | improve this answer

edited Apr 25 at 8:39

answered Apr 24 at 13:37



  • 1

    Very nice approach, missing from the other answers.
    –  billpcs
    Apr 26 at 15:40

add a comment  | 

up vote
down vote

The enumerate function works as follows:

doc = """I like movie. But I don't like the cast. The story is very nice"""
doc1 = doc.split('.')
for i in enumerate(doc1): print(i)

The output is

(0, 'I like movie')
(1, " But I don't like the cast")
(2, ' The story is very nice')

share | improve this answer

answered Feb 5 at 13:23

Ashok Kumar Jayaraman


add a comment  | 

up vote
down vote

As other users have mentioned, enumerate is a generator that adds an incremental index next to each item of an iterable.

So if you have a list say l = ["test_1", "test_2", "test_3"], the list(enumerate(l)) will give you something like this: [(0, 'test_1'), (1, 'test_2'), (2, 'test_3')].

Now, when this is useful? A possible use case is when you want to iterate over items, and you want to skip a specific item that you only know its index in the list but not its value (because its value is not known at the time).

for index, value in enumerate(joint_values): if index == 3: continue # Do something with the other `value`

So your code reads better because you could also do a regular for loop with range but then to access the items you need to index them (i.e., joint_values[i]).

Although another user mentioned an implementation of enumerate using zip, I think a more pure (but slightly more complex) way without using itertools is the following:

def enumerate(l, start=0): return zip(range(start, len(l) + start), l)


l = ["test_1", "test_2", "test_3"]
enumerate(l, 10)


[(0, ‘test_1’), (1, ‘test_2’), (2, ‘test_3’)]

[(10, ‘test_1’), (11, ‘test_2’), (12, ‘test_3’)]

share | improve this answer

answered Jul 18 at 13:43



add a comment  | 

protected by coldspeed Jun 9 at 6:18

Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count ).

Would you like to answer one of these unanswered questions instead?

Not the answer you’re looking for? Browse other questions tagged python enumerate or ask your own question .


4 years, 9 months ago


316,852 times


4 months ago


Welcome Wagon: Community and Comments on Stack Overflow



What is enumerate in Python mean?


What is the function of enumerate in my code?


how to create an incremental id for sentences?


What are ‘c’ and ‘value’ ? Can someone explain how these works?


What does the “yield” keyword do?


Detect If Item is the Last in a List


How to iterate a numpy array with index and value


Python : The second for loop is not running


Python item count in a for loop


Drop the lowest score and give back the average

see more linked questions…



What are metaclasses in Python?


What is the difference between @staticmethod and @classmethod?


What does the “yield” keyword do?


Does Python have a ternary conditional operator?


What does if __name__ == “__main__”: do?


Convert bytes to a string?


“Least Astonishment” and the Mutable Default Argument


What is the common header format of Python files?


Does Python have a string ‘contains’ substring method?


Why is “1000000000000000 in range(1000000000000001)” so fast in Python 3?

Hot Network Questions

  • Why do Republicans and others accept the propagandaic use of the term "Progressives"?

  • Can a creature with truesight see the invisible sensor created by the Clairvoyance spell?

  • What does this control do on new airplanes?

  • Using a 12 V 3 A bilge pump on a 12 V 8.5 A LED power supply

  • Why 你在笑什么?, why not 你笑在什么?

  • A definition of "Done" in case of many Development Teams working on a single product

  • Are Agent Barton and Agent Romanoff ever called ‘Hawkeye’ and ‘Black Widow’ in the MCU?

  • Does Improved Abjuration apply when casting sufficiently high level spells from scrolls?

  • Time stamps for In[] and Out[]

  • Can a puppy with its first vaccinations meet a dog that’s not fully vaccinated?

  • Digital Sumorial

  • What is the function of this complicated tensioning system?

  • An object that is gold in one specific area but becomes worthless if you leave that area?

  • Is this test for callout wrong? How to make a bulk?

  • Why aren’t Republicans more focused on mobilizing a movement towards ‘dethroning’ Trump?

  • Is a text only resume (.txt) unprofessional?

  • Is the US suffering from legislative paralysis?

  • How to remove my name off a car loan

  • minimum repayment on home loan

  • L’Hospital’s Rule application with raised exponents.

  • What does "were identical to the last freckle" mean in this context?

  • How to improve logic to check whether 4 boolean values match some cases

  • Does wire color matter in electronics?

  • Avraham’s knife was called Ma’achelet. Why?

more hot questions

question feed


Stack Overflow works best with JavaScript enabled