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.

4
  • 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). Commented Mar 1, 2012 at 21:18
  • 3
    duplicate of stackoverflow.com/questions/522563/… Commented Mar 1, 2012 at 22:19
  • 1
    You should really check the python documentation before posting here. Also, I would recommend checking out a python for programmers book, 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. Commented Mar 2, 2014 at 19:36
  • 1
    The short answer to your question is use for i in range(5). Commented Mar 2, 2014 at 19:42

6 Answers 6

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
Sign up to request clarification or add additional context in comments.

4 Comments

better to use xrange() instead of range() in this case ;)
i though range(len(my_list)) should be avoid? (for me, i would do enumerate but use only index).
@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).
By the way, you also can get the item when you're using the range() way: Just use print my_list[i].
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

Comments

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

Comments

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

Comments

0

This?

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

1 Comment

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.
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

2 Comments

In this case it will return me the items, not its indexes. =/
right - use enumerate() or for index in range(len(my_list)) then

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.