[Python-Dev] Toowtdi: Datatype conversions (original) (raw)

Guido van Rossum guido at python.org
Sat Jan 3 20:15:04 EST 2004


Choosing between:

list(d) or d.keys() Which is the one obvious way of turning a dictionary into a list? IMO, list(d) is it.

Ugh. I really hope we aren't going to teach people to write list(d) instead of d.keys(). The latter is totally clear. The former requires one to stop and remember that this uses the keys only.

This is different for sets, where there's no ambiguity in what list(d) could possibly mean (except for the ordering, which is a second-order issue that doesn't affect the type of the result).

While I like being able to write

for key in d: ...

instead of

for key in d.keys(): ...

I'm not so sure that having list(d) do anything at all was such a great idea. Not because of TOOWTDI, but because it doesn't tell the reader enough. And the polymorphism properties are really weird: if d could be either a mapping or a sequence, list(d) either loses information or it doesn't.

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



More information about the Python-Dev mailing list