[Edu-sig] Reversing dictionaries, closures, permutations etc.

Guido van Rossum guido at python.org
Fri Jan 23 20:22:58 EST 2004


> > I believe this is usually called "inverse".  In Python, "reverse"
> > means reversing the order of the elements of a list,
> > e.g. lst.reverse() and in 2.4 the reversed() built-in.
> > 
> > --Guido van Rossum (home page: http://www.python.org/~guido/)
> 
> OK.  
> 
> It's a bit of a namespace collision actually (between Python usage and the
> wider world), as there's a popular notion of a "reverse phone directory"
> wherein you lookup names by phone number instead of phone number by name
> e.g. http://www.reversephonedirectory.com/ (just found myself by number).

OTOH I believe in math you talk about the inverse function (e.g. tan
<-> atan) and not the reverse function.  And my car has a reverse. :-)

> So is 2.4 mylist.reversed() something that returns a list,
> vs. reverses it in place?  Is there a parallel mylist.sorted()?

No.  reversed() and sorted() are builtins; reversed() returns an
iterator that iterates over a sequence in reverse; sorted() returns a
new list that is sorted:

>>> for i in reversed([1,2,3]): print i

3
2
1
>>> for i in sorted([1,3,2]): print i

1
2
3
>>> 

--Guido van Rossum (home page: http://www.python.org/~guido/)



More information about the Edu-sig mailing list