Simple python iteration question

i3dmaster i3dmaster at gmail.com
Tue Aug 14 18:42:52 CEST 2007


or use its builtin enumerate function:

for i, j in enumerate(list):
     print i, j

--Jim

On Aug 14, 2007, at 9:26 AM, danmcleran at yahoo.com wrote:

> On Aug 14, 10:22 am, Bryan <b... at bcc.com> wrote:
>> Hi,
>>
>> I just started with python, and have a for loop question
>>
>> In c++ (or a number of other languages) I can do this:
>>
>> for (int i=0, j=0; i < i_len, j< j_len; ++i, ++j) {}
>>
>> If I have this in python:
>> l = ['a', 'b', 'c']
>>
>> I want to get the value and also an iterator:
>> for i,v in len(l), l:
>>         print v
>>         print i
>>
>> Or something like this without declaring the iterator outside my  
>> loop...
>>
>> How do I do this?
>> Thanks!
>
> this will get index and item at index,
>
> for i in range(0, len(l)):
>     print i
>     print l[i]
>
>
> -- 
> http://mail.python.org/mailman/listinfo/python-list




More information about the Python-list mailing list