Re: Best way to have a for-loop index?

2006-04-05 Thread andrewfelch
Sometimes C++ is the right tool/style for the job, but I don't need the
speed or efficiency of C++.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Best way to have a for-loop index?

2006-04-05 Thread nikie
Roy Smith wrote:

 In article [EMAIL PROTECTED],
  [EMAIL PROTECTED] 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... ;-)

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Best way to have a for-loop index?

2006-04-05 Thread Klaas
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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Best way to have a for-loop index?

2006-03-10 Thread Terry Hancock
On 9 Mar 2006 16:32:24 -0800
[EMAIL PROTECTED] 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 ([EMAIL PROTECTED])
Anansi Spaceworks http://www.AnansiSpaceworks.com

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Best way to have a for-loop index?

2006-03-10 Thread Steven D'Aprano
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.

-- 
http://mail.python.org/mailman/listinfo/python-list


Best way to have a for-loop index?

2006-03-09 Thread andrewfelch
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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Best way to have a for-loop index?

2006-03-09 Thread Michael Spencer
[EMAIL PROTECTED] 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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Best way to have a for-loop index?

2006-03-09 Thread Scott David Daniels
[EMAIL PROTECTED] 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
[EMAIL PROTECTED]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Best way to have a for-loop index?

2006-03-09 Thread Roy Smith
In article [EMAIL PROTECTED],
 [EMAIL PROTECTED] 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?
-- 
http://mail.python.org/mailman/listinfo/python-list