472,126 Members | 1,436 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,126 software developers and data experts.

for statement on empty iterable

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

Similar topics

7 posts views Thread by Roy Smith | last post: by
10 posts views Thread by Brian Roberts | last post: by
70 posts views Thread by py | last post: by
7 posts views Thread by Tim N. van der Leeuw | last post: by
1 post views Thread by iwl | last post: by
4 posts views Thread by loial | last post: by
23 posts views Thread by florian.loitsch | last post: by
6 posts views Thread by Michele | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.