ASPN ActiveState Programmer Network
  ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups | Web Services
SEARCH

Reference
ActivePython 2.5
What's New
What's new in Python 2.3?
Contents
1 PEP 218: A Standard Set Datatype
2 PEP 255: Simple Generators
3 PEP 263: Source Code Encodings
4 PEP 273: Importing Modules from Zip Archives
5 PEP 277: Unicode file name support for Windows NT
6 PEP 278: Universal Newline Support
7 PEP 279: enumerate()
8 PEP 282: The logging Package
9 PEP 285: A Boolean Type
10 PEP 293: Codec Error Handling Callbacks
11 PEP 301: Package Index and Metadata for Distutils
12 PEP 302: New Import Hooks
13 PEP 305: Comma-separated Files
14 PEP 307: Pickle Enhancements
15 Extended Slices
16 Other Language Changes
17 New, Improved, and Deprecated Modules
18 Pymalloc: A Specialized Object Allocator
19 Build and C API Changes
20 Other Changes and Fixes
21 Porting to Python 2.3
22 Acknowledgements
About this document ...

MyASPN >> Reference >> ActivePython 2.5 >> What's New >> What's new in Python 2.3?
ActivePython 2.5 documentation

 
7 PEP 279: enumerate()

A new built-in function, enumerate(), will make certain loops a bit clearer. enumerate(thing), where thing is either an iterator or a sequence, returns a iterator that will return (0, thing[0]), (1, thing[1]), (2, thing[2]), and so forth.

A common idiom to change every element of a list looks like this:

for i in range(len(L)):
    item = L[i]
    # ... compute some result based on item ...
    L[i] = result

This can be rewritten using enumerate() as:

for i, item in enumerate(L):
    # ... compute some result based on item ...
    L[i] = result

See Also:

PEP 279, The enumerate() built-in function
Written and implemented by Raymond D. Hettinger.

See About this document... for information on suggesting changes.

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState 2004 All rights reserved