Re: Sorting dict keys



montyphyton@xxxxxxxxx wrote:

I'd like to do it in one line because what I am trying to do is,
after all, a single, simple enough action.

No, it's two actions:

1. You extract a list of keys from the dict
2. you sort it

I find the suggested b = sorted(a.keys()) much more readable than
breaking it up in two lines. In the long run, I guess it's a
matter of personal taste...

Yes. You'll see, at least if you do a third thing with the list it
will start to become less well-readable.

Regards,


Björn

--
BOFH excuse #450:

Terrorists crashed an airplane into the server room, have to
remove /bin/laden. (rm -rf /bin/laden)

.



Relevant Pages

  • Re: Efficient processing of large nuumeric data file
    ... the test for whether a key is in the dict. ... Keep an eye on your program's memory consumption as it runs. ... I'd probably use the unix "sort" utility to sort the input (that ... each group of keys (see itertools.groupby for a convenient way to do ...
    (comp.lang.python)
  • Re: I dig stumperings.....
    ... infinite impregnability where no-one's troubled by buses. ... suggesting both within and among the keys. ... but one went sort of like this. ...
    (talk.religion.buddhism)
  • Re: Help With PyParsing of output from win32pdhutil.ShowAllProcesses()
    ... None to a string). ... Still have an urge to parse with pyparsing? ... pyparsing Dict class. ... keys from the parsed data, so it looks like you are already thinking ...
    (comp.lang.python)
  • Re: a question for sorting keys in Map
    ... I have a Map, actually a TreeMap, which will automatically sort the keys ... You can write your own Comparator object which implements the Compare ... strings so that X10 comes after X2 instead of before it. ... Unix ls command sort file names the way you want your strings to sort. ...
    (comp.lang.java.programmer)
  • Re: sorting DBM hash
    ... a DBM Hash. ... The first sort routine prints out keys and value. ... An anonymous sub routine is used to hold sub routines ...
    (perl.beginners)