47

I need to do in Python the same as:

for (i = 0; i < 5; i++) {cout << i;} 

but I don't know how to use FOR in Python to get the index of the elements in a list.

Remi Guan
  • 21,506
  • 17
  • 64
  • 87
Lucas Rezende
  • 2,516
  • 8
  • 25
  • 34
  • I didn't put the list. what I need is to simply get the index of elements in a list. Normally I'd use a FOR in C++ or Java to get this info. In this case, supposing that the list is ["a", "b", "c"], I need the index of a, b and c (0, 1 and 2). – Lucas Rezende Mar 01 '12 at 21:18
  • 3
    duplicate of http://stackoverflow.com/questions/522563/accessing-the-index-in-python-for-loops – Reinstate Monica Mar 01 '12 at 22:19
  • 1
    [You should really check the python documentation before posting here.](http://docs.python.org/2/tutorial/controlflow.html#the-range-function) Also, [I would recommend checking out a python for programmers book,](https://wiki.python.org/moin/BeginnersGuide/NonProgrammers) since you don't seem to know a lot about python but understand the basics of other languages. You can also simply read through that tutorial, it will tell you everything you need to know. – jfa Mar 02 '14 at 19:36
  • 1
    The short answer to your question is use `for i in range(5)`. – jfa Mar 02 '14 at 19:42

6 Answers6

81

If you have some given list, and want to iterate over its items and indices, you can use enumerate():

for index, item in enumerate(my_list):
    print index, item

If you only need the indices, you can use range():

for i in range(len(my_list)):
    print i
Sven Marnach
  • 574,206
  • 118
  • 941
  • 841
  • better to use xrange() instead of range() in this case ;) – n1r3 Mar 01 '12 at 21:22
  • i though `range(len(my_list))` should be avoid? (for me, i would do `enumerate` but use only `index`). – neizod Mar 01 '12 at 21:24
  • @neizod: The case that you only need the indices is actually rather rare, but if this is all you need, there is no reason to avoid `range(len(my_list))` (or `xrange(len(my_list))` in Python 2.x). – Sven Marnach Mar 01 '12 at 21:27
  • By the way, you also can get the item when you're using the `range()` way: Just use `print my_list[i]`. – Remi Guan Dec 07 '15 at 05:34
22

Just use

for i in range(0, 5):
    print i

to iterate through your data set and print each value.

For large data sets, you want to use xrange, which has a very similar signature, but works more effectively for larger data sets. http://docs.python.org/library/functions.html#xrange

dangerChihuahua007
  • 20,299
  • 35
  • 117
  • 206
18

If you have an existing list and you want to loop over it and keep track of the indices you can use the enumerate function. For example

l = ["apple", "pear", "banana"]
for i, fruit in enumerate(l):
   print "index", i, "is", fruit
pkit
  • 7,993
  • 6
  • 36
  • 36
10

use enumerate:

>>> l = ['a', 'b', 'c', 'd']
>>> for index, val in enumerate(l):
...    print "%d: %s" % (index, val)
... 
0: a
1: b
2: c
3: d
bgporter
  • 35,114
  • 8
  • 59
  • 65
0

This?

for i in range(0,5): 
 print(i)
Danny
  • 7,368
  • 8
  • 46
  • 70
  • This would work for me because I already know that I will have always 5 items. :) But in other cases I'd need to get the indexes of unknown number of elements. – Lucas Rezende Mar 01 '12 at 21:27
0

In additon to other answers - very often, you do not have to iterate using the index but you can simply use a for-each expression:

my_list = ['a', 'b', 'c']
for item in my_list:
    print item
alex
  • 1,229
  • 7
  • 6