[Tutor] Sorting a dictionary in one line?

Magnus Lycka magnus@thinkware.se
Mon Jan 20 10:52:00 2003


At 14:16 2003-01-20 +0100, Charlie Clark wrote:
>Agreed in general though some of the newer functions such as list
>comprehensions are opening this up.

Yes. map, reduce, filter and lambda were odd components
from early versions, and it seems GvR feels that it might
not have been a good idea to bring them into Python. The
same could be said about list comprehension.

But on the other hand, we shouldn't choose syntax just
based on what syntax is common in older languages. All
concepts, whether it's assignments, for-loops or list
comprehensions have to be learned.

While list comprehension is a redundant construction, it
does give a lot in a compact form. If we should remove
something today, I can agree with the commonly held opinion
that it would be better to through out the old lambda, map,
filter and reduce, and keepp list comprehension. (Not that
list comprehension replaces reduce or all uses of lambda...)

List comprehension is at least nothing entirely new. The
concept that you can create a new list inside [ and ] by
performing operations on an existing list using a for loop
fits in with what we already know about the pieces [, ],
for, in and if...

>I thought about the following thing:
>d.keys().sort()
>should sort the implicit list d.keys() and is certainly valid Python but it
>doesn't give you what you expect. I guess d.keys() creates a list on the
>fly from the dictionary keys so unless you use it immediately or assign it,
>it disappears straight away.

Yes, something like that.

It's a bit like typing

6 * 7

on a line by it's own. It works, but what good will it do?

>I know I've had it explained to me but I still have trouble with it.
>What does d.keys() return?

Why ask when you can run a python interpreter on your computer?

 >>> print a.keys()
???

>If I assign an object to d.keys() what is actually assigned? ie., what
>happens when the dictionary is updated?

What do you mean by that? Assign an object to d.keys()? d.keys() returns
an object, a list object to be precise. With an assignment, you can make
a variable refer to that object. I.e. k = d.keys(). Is that what you meant?
I suggest you fire up Python and experiment. It's all fairly logical...

>d = ['b':2, 'c':3, '2']

What did you try to do here? A listionary? ;)

>a = d.keys()
>d['d'] = 4
>
>what will print a give and why?

     d = ['b':2, 'c':3, '2']
             ^
SyntaxError: invalid syntax

May I suggest that you sit with the python interpreter available when
you write your emails. Type your code in python, not in your mail
client, and copy code from the python interpreter to the email. That
will reduce the mistypings, and you won't have to ask about things
that are obvious if you try for yourself...


-- 
Magnus Lycka, Thinkware AB
Alvans vag 99, SE-907 50 UMEA, SWEDEN
phone: int+46 70 582 80 65, fax: int+46 70 612 80 65
http://www.thinkware.se/  mailto:magnus@thinkware.se