By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,821 Members | 1,092 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,821 IT Pros & Developers. It's quick & easy.

for statement on empty iterable

P: n/a
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

Aug 22 '07 #1
Share this Question
Share on Google+
11 Replies

P: n/a
james_027 <ca********@gmail.comwrites:
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.
Aug 22 '07 #2

P: n/a
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
Aug 22 '07 #3

P: n/a
james_027 <ca********@gmail.comwrites:
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.
Aug 22 '07 #4

P: n/a
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

Aug 22 '07 #5

P: n/a
On 8/22/07, james_027 <ca********@gmail.comwrote:
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
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.
Aug 22 '07 #6

P: n/a
james_027 <ca********@gmail.comwrites:
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 ;).
Aug 22 '07 #7

P: n/a
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

Aug 22 '07 #8

P: n/a
james_027 schrieb:
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.
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
Aug 22 '07 #9

P: n/a
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
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

--
http://mail.python.org/mailman/listinfo/python-list
Aug 22 '07 #10

P: n/a
Amit Khemka wrote:
On 8/22/07, james_027 <ca********@gmail.comwrote:
>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

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:
>>for x in enumerate("James"):
.... 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 -------------

Aug 22 '07 #11

P: n/a
On 2007-08-22, Diez B. Roggisch <de***@nospam.web.dewrote:
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.

--
Neil Cerutti
A billion here, a billion there, sooner or later it adds up to real money.
--Everett Dirksen
Aug 22 '07 #12

This discussion thread is closed

Replies have been disabled for this discussion.