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

Simple python iteration question

P: n/a
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!
Aug 14 '07 #1
Share this Question
Share on Google+
13 Replies


P: n/a
On Aug 14, 10:22 am, Bryan <b...@bcc.comwrote:
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[i]
Aug 14 '07 #2

P: n/a
Bryan <b@bcc.comwrote:
How do I do this?
for i, item in enumerate(l):
print i, item
--
Lawrence, oluyede.org - neropercaso.it
"It is difficult to get a man to understand
something when his salary depends on not
understanding it" - Upton Sinclair
Aug 14 '07 #3

P: n/a
On Tue, Aug 14, 2007 at 12:22:04PM -0400, Bryan wrote:
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

--

[Will Maier]-----------------[wi*******@ml1.net|http://www.lfod.us/]
Aug 14 '07 #4

P: n/a
On Aug 14, 11:27 am, ra...@dot.com (Lawrence Oluyede) wrote:
Bryan <b...@bcc.comwrote:
How do I do this?

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!
>>help(enumerate)
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

Aug 14 '07 #5

P: n/a
On 8/14/07, Bryan <b@bcc.comwrote:
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!
--
http://mail.python.org/mailman/listinfo/python-list

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
>>>
Aug 14 '07 #6

P: n/a
or use its builtin enumerate function:

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

--Jim

On Aug 14, 2007, at 9:26 AM, da********@yahoo.com wrote:
On Aug 14, 10:22 am, Bryan <b...@bcc.comwrote:
>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[i]
--
http://mail.python.org/mailman/listinfo/python-list
Aug 14 '07 #7

P: n/a
<snip>
this will get index and item at index,

for i in range(0, len(l)):
print i
print l[i]
--
http://mail.python.org/mailman/listinfo/python-list

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.
Aug 14 '07 #8

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

--

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
Aug 14 '07 #9

P: n/a
In article <ma***************************************@python. org>,
Shawn Milochik <Sh***@Milochik.comwrote:
Aug 15 '07 #10

P: n/a
On Aug 14, 11:59 am, "Shawn Milochik" <Sh...@Milochik.comwrote:
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
Aug 15 '07 #11

P: n/a
En Wed, 15 Aug 2007 10:37:16 -0300, Cameron Laird <cl****@lairds.us>
escribi�:
Shawn Milochik <Sh***@Milochik.comwrote:
>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, 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)

--
Gabriel Genellina

Aug 15 '07 #12

P: n/a
Dennis Lee Bieber schreef:
On Wed, 15 Aug 2007 13:39:57 -0300, "Gabriel Genellina"
<ga*******@yahoo.com.ardeclaimed the following in comp.lang.python:
>"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).
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:V..._b3_v_sst.jpg), Mercedes
(e.g. http://www.mediaweb06.com/diffusion/.../033f1ccd.jpg), BMW
(e.g. http://www.degrifcars.com/images/BMW...es3_break.jpg),
Renault (e.g.
http://www.carfolio.com/images/dbima...1.9_rxe_d.jpg),
Ford (e.g. http://en.wikipedia.org/wiki/Image:F...cus-wagon.jpg),
Toyota (e.g.
http://www.outrefranc.com/modeles/to...ollabreak3.jpg) and many
others.

--
If I have been able to see further, it was only because I stood
on the shoulders of giants. -- Isaac Newton

Roel Schroeven
Aug 15 '07 #13

P: n/a
In article <11**********************@e9g2000prf.googlegroups. com>,
BartlebyScrivener <bs**********@gmail.comwrote:
>On Aug 14, 11:59 am, "Shawn Milochik" <Sh...@Milochik.comwrote:
>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

.... 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.
Aug 15 '07 #14

This discussion thread is closed

Replies have been disabled for this discussion.