for statement on empty iterable
Diez B. Roggisch
deets at nospam.web.de
Wed Aug 22 09:16:40 CEST 2007
>> Oh I see. You have to combine a couple of concepts but for this
>> example you'd say:
>> name = 'james' # 'l' looks too much like the digit 1
>> for i,c in enumerate(name):
>> print i, c
>> print i
>> enumerate(name) generates the sequence
>> (0,'j'), (1,'a'), (2,'m'), (3,'e'), (4,'s')
>> and the above loop splits those tuples into two indexes i and c.
>> You should probably read the tutorial and work through the examples.
>> If you get something wrong with this basic stuff, your computer won't
>> explode or anything like that, so try stuff out. Be more careful when
>> you start using library routines that can delete files ;).
> Thanks, it just that I am afraid of coding that something works but
> ugly or inefficient.
While it is desireable to not only write working, but also aesthetically
pleasing code, as a beginner you shouldn't worry too much. The sense of
aesthetics develops with time. Important is to try and grasp the idioms
of the language, around here ofter referred to as "beeing pythonic."
For example, the concept of iterators, and that for most of the times
you don't need or want an index. And if you really need it, create it
like paul showed you above.
More information about the Python-list