Three dumb questions (ordered by dumbness descending)
imbosol at vt.edu
Mon Sep 23 23:16:15 EDT 2002
Thorsten Kampe wrote:
> Okay, here they are:
> 1. Why is 'zip(zip(x)) != x' (should be the same because it's the
Because zip doesn't transpose. If zip were a transpose, it would take
a *single* argument, which is a sequence of sequences, and would
return the sequences transposed. But zip doesn't do that. Rather, it
takes *multiple* arguments, all of which must be sequences, and
returns a list of tuples of the nth item of the arguments.
Maybe you'll understand it this way, if you're a LISP programmer:
zip = lambda x: apply(transpose,x)
where transpose is the function that does what you think zip does.
> 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)?
A common one is "don't use 'in' in a loop". The 'in' operator (for
regular lists) does a linear search of the list. I did this recently
for an file sync'ing program and that part goes slow.
Really new versions of Python define an 'in' operation for dicts also,
but that's ok to use in a loop because it does a hash lookup.
More information about the Python-list