Simple python iteration question

B

Bryan

Hi,

I just started with python, and have a for loop question

In c++ (or a number of other languages) I can do this:

for (int i=0, j=0; i < i_len, j< j_len; ++i, ++j) {}

If I have this in python:
l = ['a', 'b', 'c']

I want to get the value and also an iterator:
for i,v in len(l), l:
print v
print i

Or something like this without declaring the iterator outside my loop...

How do I do this?
Thanks!
 
D

danmcleran

Hi,

I just started with python, and have a for loop question

In c++ (or a number of other languages) I can do this:

for (int i=0, j=0; i < i_len, j< j_len; ++i, ++j) {}

If I have this in python:
l = ['a', 'b', 'c']

I want to get the value and also an iterator:
for i,v in len(l), l:
print v
print i

Or something like this without declaring the iterator outside my loop...

How do I do this?
Thanks!

this will get index and item at index,

for i in range(0, len(l)):
print i
print l
 
W

Will Maier

I just started with python, and have a for loop question

In c++ (or a number of other languages) I can do this:

for (int i=0, j=0; i < i_len, j< j_len; ++i, ++j) {}

If I have this in python:
l = ['a', 'b', 'c']

I want to get the value and also an iterator:
for i,v in len(l), l:
print v
print i

Or something like this without declaring the iterator outside my loop...

How do I do this?

Use the enumerate() builtin.
>>> l = ['a', 'b', 'c']
>>> for i, v in enumerate(l):
... print i, v
...
0 a
1 b
2 c
 
P

programmer.py

for i, item in enumerate(l):
print i, item

^^ That is the `most-correct` answer. But because you're new, I'll
take this time to introduce you to help(), just in case you're too
lazy to RTFM (like me ;)). help works on objects, and things you may
not expect - try help(2). Oh and welcome to `fun` programming!

class enumerate(object)
| enumerate(iterable) -> iterator for index, value of iterable
|
| Return an enumerate object. iterable must be an other object that
supports
| iteration. The enumerate object yields pairs containing a count
(from
| zero) and a value yielded by the iterable argument. enumerate is
useful
| for obtaining an indexed list: (0, seq[0]), (1, seq[1]), (2,
seq[2]), ...

... and a bunch of stuff I clipped ...

hth,
jw
 
S

Shawn Milochik

Hi,

I just started with python, and have a for loop question

In c++ (or a number of other languages) I can do this:

for (int i=0, j=0; i < i_len, j< j_len; ++i, ++j) {}

If I have this in python:
l = ['a', 'b', 'c']

I want to get the value and also an iterator:
for i,v in len(l), l:
print v
print i

Or something like this without declaring the iterator outside my loop...

How do I do this?
Thanks!


If I understand properly, maybe enumerate will help you:


a = ['a','b','c']
for i,v in enumerate(a):
.... print i
.... print v
....
0
a
1
b
2
c
 
I

i3dmaster

or use its builtin enumerate function:

for i, j in enumerate(list):
print i, j

--Jim

Hi,

I just started with python, and have a for loop question

In c++ (or a number of other languages) I can do this:

for (int i=0, j=0; i < i_len, j< j_len; ++i, ++j) {}

If I have this in python:
l = ['a', 'b', 'c']

I want to get the value and also an iterator:
for i,v in len(l), l:
print v
print i

Or something like this without declaring the iterator outside my
loop...

How do I do this?
Thanks!

this will get index and item at index,

for i in range(0, len(l)):
print i
print l
 
S

Shawn Milochik

this will get index and item at index,

for i in range(0, len(l)):
print i
print l



Enumerate is better here -- it provides the same result and that's
what it's for. However, if you do use range, the zero is unnecessary
-- beginning at zero is the default.
 
C

Cameron Laird

.
.
.
Just for my own sanity: Isn't this the third response advocating the
use of enumerate()? Did the other responses not get through, or was
this a time-delay thing?
.
.
.
Yes.

Yes, for a variety of reasons, it's easy for it to happen that
all three-or-more enumerate-responders independently saw the
original question, but not any of their colleagues' responses.

Sometime we'll tell a few Usenet funnies about The Old Days
when transport included tape-backup-driven-by-private-car.
 
B

BartlebyScrivener

Just for my own sanity: Isn't this the third response advocating the
use of enumerate()? Did the other responses not get through, or was
this a time-delay thing?

Thanks,
Shawn

Look at the timestamps. All within ten minutes. And those ten minutes
are spent keyboarding your response in and posting, plus it takes
several minutes for the posts to appear on Google Groups.

rd
 
G

Gabriel Genellina

Yes, for a variety of reasons, it's easy for it to happen that
all three-or-more enumerate-responders independently saw the
original question, but not any of their colleagues' responses.

Sometime we'll tell a few Usenet funnies about The Old Days
when transport included tape-backup-driven-by-private-car.

"Never underestimate the bandwidth of a station wagon full of tapes
hurtling down the highway"
(Andrew S. Tanenbaum, Computer Networks, 1996; maybe the origin is much
older)
 
D

Dennis Lee Bieber

"Never underestimate the bandwidth of a station wagon full of tapes
hurtling down the highway"
(Andrew S. Tanenbaum, Computer Networks, 1996; maybe the origin is much
older)

Considering how few "proper" "station wagons" were manufactured at
that time period... Volvo was about it -- all the other makers tended to
push "mini-vans"... Besides Volvo, I think the only current production
station wagon is the Dodge Magnum (and that looks more like a
chopped/channeled surfer hot-rod).
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
R

Roel Schroeven

Dennis Lee Bieber schreef:
Considering how few "proper" "station wagons" were manufactured at
that time period... Volvo was about it -- all the other makers tended to
push "mini-vans"... Besides Volvo, I think the only current production
station wagon is the Dodge Magnum (and that looks more like a
chopped/channeled surfer hot-rod).

At least in Europe (including the Netherlands where Tanenbaum lives),
station wagons were still popular then. Minivans have displaced them
somewhat since then, but by 1996 there were still quite a few station
wagons on the roads. There still are quite a few of them around; my boss
just bought a new Saab 9-3 station wagon.

Manufacturers who made (and make) station wagons include VW (e.g.
http://en.wikipedia.org/wiki/Image:Vw_passat_b3_v_sst.jpg), Mercedes
(e.g. http://www.mediaweb06.com/diffusion/annonces/033f1ccd.jpg), BMW
(e.g. http://www.degrifcars.com/images/BMW/Series3/b_Series3_break.jpg),
Renault (e.g.
http://www.carfolio.com/images/dbimages/zgas/models/id/9924/megane_break_1.9_rxe_d.jpg),
Ford (e.g. http://en.wikipedia.org/wiki/Image:Ford-Focus-wagon.jpg),
Toyota (e.g.
http://www.outrefranc.com/modeles/toyota/img/corollabreak3.jpg) and many
others.
 
C

Cameron Laird

Look at the timestamps. All within ten minutes. And those ten minutes
are spent keyboarding your response in and posting, plus it takes
several minutes for the posts to appear on Google Groups.

rd

.... and now we've had two people reply with more-or-less the same
message to this question about the propriety of race conditions in
replies. Go ahead, explain *that* level of abstracted confusion
to your civilian acquaintances in The Real World.
 

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
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top