473,507 Members | 6,727 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1013
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
11320
by: Roy Smith | last post by:
In the old days, if I wanted to return a sequence of items, I'd return a list, and loop over it like this: for thing in getListOfThings (): do something With iterators, I'm doing: for...
10
7762
by: Brian Roberts | last post by:
I'm using using generators and iterators more and more intead of passing lists around, and prefer them. However, I'm not clear on the best way to detect an empty generator (one that will return no...
70
3970
by: py | last post by:
I have function which takes an argument. My code needs that argument to be an iterable (something i can loop over)...so I dont care if its a list, tuple, etc. So I need a way to make sure that...
7
52996
by: Tim N. van der Leeuw | last post by:
Hi, I'd like to know if there's a way to check if an object is a sequence, or an iterable. Something like issequence() or isiterable(). Does something like that exist? (Something which, in...
1
1684
by: iwl | last post by:
Hi, I've tryed to save some data containing empty arrays (array('f')) in a shelve. It looks like the shelve has some problems with empty arrays, get allways: TypeError: ("'NoneType' object is...
4
3909
by: loial | last post by:
I am writing a file in python with writelines f = open('/home/john/myfile',"w") f.writelines("line1\n") f.writelines("line2\n") f.close() But whenever I try to do anything with the file in...
23
2042
by: florian.loitsch | last post by:
According to the spec Section 14 the production SourceElements:SourceElements SourceElement is evaluated as follows: 1. Evaluate SourceElements. 2. If Result(1) is an abrupt completion, return...
11
1386
by: Erich | last post by:
Hello all, Today I found myself once again defining two functions that I use all the time: nsplit and iterable. These little helper functions of mine get used all the time when I work. Im sick...
6
2222
by: Michele | last post by:
Hi there, I'm relative new to Python and I discovered that there's one single way to cycle over an integer variable with for: for i in range(0,10,1) which is equivalent to: for (i = 0; i < 10;...
0
7109
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7372
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7029
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7481
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5619
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3190
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1537
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.