for statement on empty iterable

J

james_027

hi,

I need to do some for loop on iterables which could be empty
sometimes, for example

a_list = []

for i in a_list:
#do something

is this safe? or should I put a if statement to test it first?

Thanks
james
 
P

Paul Rubin

james_027 said:
for i in []:
#do something
is this safe? or should I put a if statement to test it first?

That doesn't crash or anything like that, but it also doesn't
set the index variable, which can cause confusion in some situations.
 
J

james_027

hi Paul,
That doesn't crash or anything like that, but it also doesn't
set the index variable, which can cause confusion in some situations.

Thanks for your quick answer ... Actually I was thinking how do I
access the index inside a for statement? Can you help

Thanks
james
 
P

Paul Rubin

james_027 said:
Thanks for your quick answer ... Actually I was thinking how do I
access the index inside a for statement? Can you help

It sounds like you're just starting to learn the language... have you
read the online tutorial yet? That is a pretty easy introduction.

See: http://python.org/doc/

Anyway, you can say

for i in (1,2,3):
print i*5

to print 5, 10, and 15 on separate lines, for example.
 
J

james_027

hi,
It sounds like you're just starting to learn the language... have you
read the online tutorial yet? That is a pretty easy introduction.

See:http://python.org/doc/

Anyway, you can say

for i in (1,2,3):
print i*5

to print 5, 10, and 15 on separate lines, for example.

Yes i am new to python :). I am sorry I should be clarify myself ...
for example

l = ['j', 'a', 'm', 'e', 's']

for i in l
# i want to know the nth number of times it has loop thru or
something like counter?

Thanks
james
 
A

Amit Khemka

hi Paul,


Thanks for your quick answer ... Actually I was thinking how do I
access the index inside a for statement? Can you help

Have a look at "enumerate", You can iterate over a list like:

for i,x in enumerate(['a', 'b', 'c']):
print i, x

It prints:
0 a
1 b
2 c

cheers,
amit

----
Amit Khemka
website: www.onyomo.com
wap-site: www.owap.in
Home Page: www.cse.iitd.ernet.in/~csd00377

Endless the world's turn, endless the sun's Spinning, Endless the quest;
I turn again, back to my own beginning, And here, find rest.
 
P

Paul Rubin

james_027 said:
Yes i am new to python :). I am sorry I should be clarify myself ...
for example

l = ['j', 'a', 'm', 'e', 's']

for i in l
# i want to know the nth number of times it has loop thru or
something like counter?

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 ;).
 
J

james_027

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

cheers,
james
 
D

Diez B. Roggisch

james_027 said:
hi,


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.

Diez
 
E

Eric Abrahamsen

Here's another simple method:

l = ['j', 'a', 'm', 'e', 's']
counter = 0

for i in l:
# Do your code
counter += 1

print counter

Yrs,
Eric
 
S

Steve Holden

Amit said:
hi Paul,

Thanks for your quick answer ... Actually I was thinking how do I
access the index inside a for statement? Can you help

Have a look at "enumerate", You can iterate over a list like:

for i,x in enumerate(['a', 'b', 'c']):
print i, x

It prints:
0 a
1 b
2 c
Also remember that string are iterable:
.... print x
....
(0, 'J')
(1, 'a')
(2, 'm')
(3, 'e')
(4, 's')
regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------
 
N

Neil Cerutti

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

It's hard to keep up with the new buzzwords that keep popping up
in this group.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top