# an ingrate newbie complains

Skip Montanaro skip at pobox.com
Wed Feb 4 20:28:44 CET 2004

```    Elaine> 1) I find the following behavior puzzling and disappointing:

>>> X=[(1,1),(2,4),(3,9),(4,16),(5,25)]
>>> Y=dict(X)
>>> Z=list(Y)
>>> Z==X
False
>>> Z==Y.keys()
True

That list(Y) returns the keys of X is perhaps unfortunate, but the same
behavior allows you to write:

for key in Y:
print (key, y[key])

which can be an efficiency gain if Y is large (not having to build a list of
all the keys ahead of time).  You'll find this to be true though:

W = Y.items()
W.sort()
W == X

Elaine> 2) How come you can't write...

Elaine> if y = f(x) > 3:
Elaine>     <do a bunch of things with x and y>

There is a common class of errors in C code:

if (c = 0) {
...
}

Is that supposed to be an assignment or a test?  Python avoids that problem
by not allowing assignments within expressions.

Elaine> That kind of syntax would be especially welcome in list
Elaine> comprehensions:

Elaine> [f(x,y) for x in list if y=g(x)<=0]

I think you can recast that as:

[f(x,y) for (x,y) in zip(list, [g(z) for z in list]) if y <= 0]

but do you really want to?  (This won't work if list is an iterator.)

Elaine> If g(x) is a fairly complicated expression, and y occurs several
Elaine> times in f(x,y), considerable clarity could be gained.

If g(x) is a fairly complicated expression and y occurs several times in
f(x,y), perhaps you should be using a for loop instead of a list
comprehension:

result = []
for x in list:
y = g(x)
if y <= 0:
result.append(f(x,y))
return result

(FYI, you shouldn't use "list" as a variable name.  You're shadowing a
builtin, a practice that can lead to confusing error messages, if nothing
else.)

Skip

```