Three dumb questions (ordered by dumbness descending)

Greg Ewing see_reply_address at something.invalid
Tue Sep 24 23:07:04 EDT 2002

Thorsten Kampe wrote:

> 1. Why is 'zip(zip(x)) != x' (should be the same because it's the 
> transposed)

zip(x) isn't the transpose of x. Try it and you'll see.

However, zip(*x) IS the transpose of x (or apply(zip, x) in
older Pythons).


    zip(*zip(*x)) == x

is true, provided you start with an x of the right type
(a list of tuples, as opposed to a list of lists or
tuple of tuples, etc.)

> 2. Peter Norvig mentions in "Python for Lisp Programmers" some 
> "don'ts": "[x] + y" and "x[1:]". Are there more things to avoid 
> (especially in a loop)?

The "for Lisp Programmers" is the important thing here.
He's pointing out that these operations are not as
efficient as the corresponding ones in Lisp, because
Python lists aren't linked lists like Lisp lists, so
these operations involve copying a whole list.

> So: can I use random.shuffle or do I have to write my own - slower - 
> version to get a random list?

It depends on how imporant it is to you that all
possible permutations have equal probability. If
you're writing a solitaire game, probably nobody
will notice the difference. If it's a nuclear
missile control system, you might have to take
more care...

Greg Ewing, Computer Science Dept,
University of Canterbury,	
Christchurch, New Zealand

More information about the Python-list mailing list