I write a lot of code that looks like this:
for myElement, elementIndex in zip( elementList,
range(len(elementList))):
print "myElement ", myElement, " at index: ",elementIndex
My question is, is there a better, cleaner, or easier way to get at the
element in a list AND the index of a loop than this?
TIA,
Andrew 8 1513 an*********@gmail.com wrote: I write a lot of code that looks like this:
for myElement, elementIndex in zip( elementList, range(len(elementList))): print "myElement ", myElement, " at index: ",elementIndex
My question is, is there a better, cleaner, or easier way to get at the element in a list AND the index of a loop than this?
for elementIndex, myElement in enumerate(elementList):
print "myElement ", myElement, " at index: ",elementIndex
--Scott David Daniels sc***********@acm.org an*********@gmail.com wrote: I write a lot of code that looks like this:
for myElement, elementIndex in zip( elementList, range(len(elementList))): print "myElement ", myElement, " at index: ",elementIndex
My question is, is there a better, cleaner, or easier way to get at the element in a list AND the index of a loop than this?
TIA, Andrew
enumerate(iterable)
Return an enumerate object. iterable must be a sequence, an iterator, or some
other object which supports iteration. The next() method of the iterator
returned by enumerate() returns a tuple containing a count (from zero) and the
corresponding value obtained from iterating over iterable. enumerate() is useful
for obtaining an indexed series: (0, seq[0]), (1, seq[1]), (2, seq[2]), .... New
in version 2.3
Michael
In article <11********************@j33g2000cwa.googlegroups.c om>, an*********@gmail.com wrote: I write a lot of code that looks like this:
for myElement, elementIndex in zip( elementList, range(len(elementList))): print "myElement ", myElement, " at index: ",elementIndex
My question is, is there a better, cleaner, or easier way to get at the element in a list AND the index of a loop than this?
TIA, Andrew
The real question is *why* do you want the index?
If you're trying to iterate through list indicies, you're probably trying
to write C, C++, Fortran, Java, etc in Python.
Can you describe exactly what it is that you're trying to do?
On 9 Mar 2006 16:32:24 -0800 an*********@gmail.com wrote: I write a lot of code that looks like this:
for myElement, elementIndex in zip( elementList, range(len(elementList))): print "myElement ", myElement, " at index: ",elementIndex
My question is, is there a better, cleaner, or easier way to get at the element in a list AND the index of a loop than this?
In fact it is so common a need, there is a built-in
function called "enumerate" that does the 'zip' for
you:
for elementIndex, myElement in enumerate(elementList):
print "myElement ", myElement, " at index: ",elementIndex
--
Terry Hancock (ha*****@AnansiSpaceworks.com)
Anansi Spaceworks http://www.AnansiSpaceworks.com
On Thu, 09 Mar 2006 20:22:34 -0500, Roy Smith wrote: My question is, is there a better, cleaner, or easier way to get at the element in a list AND the index of a loop than this?
TIA, Andrew
The real question is *why* do you want the index?
If you're trying to iterate through list indicies, you're probably trying to write C, C++, Fortran, Java, etc in Python.
That's a bit harsh, surely? Well-meaning, but still harsh, and untrue.
Wanting to walk through a list replacing or modifying some or all items in
place is not unpythonic. Sure, you could simply create a new list:
L = [1, 2, 3, 4]
newL = []
for item in L:
if item % 3 == 0:
newL.append(item)
else:
newL.append(item**2)
but that's wasteful if the list is big, or if the items are expensive to
copy.
Isn't this more elegant, and Pythonic?
L = [1, 2, 3, 4]
for index, item in enumerate(L):
if item % 3 != 0:
L[index] = item**2
--
Steven.
Sometimes C++ is the right tool/style for the job, but I don't need the
speed or efficiency of C++.
Roy Smith wrote: In article <11********************@j33g2000cwa.googlegroups.c om>, an*********@gmail.com wrote:
I write a lot of code that looks like this:
for myElement, elementIndex in zip( elementList, range(len(elementList))): print "myElement ", myElement, " at index: ",elementIndex
My question is, is there a better, cleaner, or easier way to get at the element in a list AND the index of a loop than this?
TIA, Andrew
The real question is *why* do you want the index?
If you're trying to iterate through list indicies, you're probably trying to write C, C++, Fortran, Java, etc in Python.
Interesting. I just wrote some tools today that parse through a bunch
of logfiles and print out something like: "unmatched memory allocation
in line XXX", or something like that. All of them have a main loop like
this:
for lineNumber, line in enumerate(file("some.log")): ...
I don't think there's anything wrong with that, is there a better way
to do it?
Personally, I don't think "enumerate" would be there if it always
encouraged an "unpythonic" programming style. But then again, I'm not
dutch, so I couldn't tell... ;-)
roy spewed: The real question is *why* do you want the index?
If you're trying to iterate through list indicies, you're probably trying to write C, C++, Fortran, Java, etc in Python.
Could we stop the stupid continual beratement of people validly asking
about enumerate()? Yes, we want to discourage:
for i in xrange(len(seq)):
seq[i]
but in this case, and many other cases, that is clearly not the
question being posed.
enumerate is one of the most useful built-ins and a love the way it
reads in code. Stop the index-hate.
-Mike This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: wm2004 |
last post by:
Which is the best C++ Compiler?
Get An Online Business and Make Money!
Learn the secrets of many ordinary people who quit their day jobs to
pursue an online business. There are many affiliate...
|
by: John J |
last post by:
I've written the following code into a class to search for and display the
results of all races entered (The complete code is in a previous thread). I
wish to amend the code so as to display the...
|
by: PENGUINS ARE THE BEST BIRDS PENGUINS ARE THE BEST |
last post by:
PENGUINS ARE THE BEST BIRDS PENGUINS ARE THE BEST BIRDS PENGUINS ARE
THE BEST BIRDS PENGUINS ARE THE BEST BIRDS
|
by: Matt Kruse |
last post by:
http://www.JavascriptToolbox.com/bestpractices/
I started writing this up as a guide for some people who were looking for
general tips on how to do things the 'right way' with Javascript. Their...
|
by: Irene |
last post by:
Hi all,
I have set up a simple VB program (and later on an ASP interface) to
manage an Athletics database. I'm using Access 2000.
To simplify, I have the Athlets, the Competitions and the...
| |
by: xenophon |
last post by:
I have a class that is used in an ASP.NET app, a WinForms app, and a
Win32 Service. What is the best way to tell what environment the code
is currently instanced in?
Thanks.
|
by: Military Smurf |
last post by:
I'm interested in writing a web service that will return more than one value.
What's the best way to accomplish this? I was wondering if an array would
be best, but I'd like to know if there is a...
|
by: shrini |
last post by:
Friends.
I need a inventory software in php for my friend's general merchant
shop.
please recommand which one is best.
In sourceforde and freshmeat, i find a lot.
but, unable to try many.
...
|
by: ankitmathur |
last post by:
Hi,
I'm trying to overcome a situation whereby I have to search through 4-5 columns and produce the results with an order by according to the values matched in these columns.
Example:
My...
|
by: vgnsh2 |
last post by:
C is the best because the hackers is most use the C lang
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
| |
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...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
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...
|
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,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |