473,320 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Simple python iteration question

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
13 1397
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
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
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
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
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
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
<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
<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
In article <ma***************************************@python. org>,
Shawn Milochik <Sh***@Milochik.comwrote:
Aug 15 '07 #10
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
by: mike420 | last post by:
Ladies and Gentlemen, I present to you the final and ultimate proof of Python's brain-damage: As you may remember, flist = for i in range(3)
21
by: Anthony Baxter | last post by:
On behalf of the Python development team and the Python community, I'm happy to announce the first alpha of Python 2.4. Python 2.4a1 is an alpha release. We'd greatly appreciate it if you could...
9
by: Xah Lee | last post by:
here's a interesting real-world algoritm to have fun with. attached below is the Perl documentation that i wrote for a function called "reduce", which is really the heart of a larger software. ...
105
by: Christoph Zwerschke | last post by:
Sometimes I find myself stumbling over Python issues which have to do with what I perceive as a lack of orthogonality. For instance, I just wanted to use the index() method on a tuple which does...
0
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 364 open ( +2) / 3769 closed ( +3) / 4133 total ( +5) Bugs : 986 open (+18) / 6701 closed ( +9) / 7687 total (+27) RFE : 258 open...
206
by: WaterWalk | last post by:
I've just read an article "Building Robust System" by Gerald Jay Sussman. The article is here: http://swiss.csail.mit.edu/classes/symbolic/spring07/readings/robust-systems.pdf In it there is a...
2
by: Chris | last post by:
I have a Bayesian simulation package that keeps running into memory allocation problems. I have a feeling this has something to do with Python (2.5.1.1) not freeing memory. The program essentially...
30
by: galiorenye | last post by:
Hi, Given this code: A** ppA = new A*; A *pA = NULL; for(int i = 0; i < 10; ++i) { pA = ppA; //do something with pA
7
by: jmDesktop | last post by:
From the Python.org tutorial: .... for x in range(2, n): .... if n % x == 0: .... print n, 'equals', x, '*', n/x .... break .... else: .... #...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.