What's the neatest way of getting dictionary entries in a specified order?

Steve D'Aprano steve+python at pearwood.info
Fri Mar 10 18:06:19 EST 2017

On Fri, 10 Mar 2017 08:53 pm, Ethan Furman wrote:

> On 03/10/2017 12:43 AM, Chris Green wrote:
>> Erik <python at lucidity.plus.com> wrote:
>>> On 09/03/17 13:09, Chris Green wrote:
>>>> Michael Torrie <torriem at gmail.com> wrote:
>>>>> On 03/08/2017 12:27 PM, Chris Green wrote:
>>>>>> I have a fairly simple application that populates a GUI window with
>>>>>> fields from a database table.  The fields are defined/configured by a
>>>>>> dictionary as follows:-
>>>>> Instead of ordering the data in Python, why not rely on the GUI to do
>>>>> the sort?  Most GUI's even have table widgets that let you click on
>>>>> the headings to sort arbitrarily.
>>>> How would it sort it?  The order I want is arbitrary, or at least not
>>>> alphabetical or anything, I just want the order to be the way I
>>>> listed it.
>>> To be fair to Michael, you did not state your required order in the
>>> message that he replied to. You just kept saying "the order I want".
>>> If you're going to make people guess, don't chastise them for guessing
>>> wrong :D
>> OK.  :-)
>> However how else can one describe an order which isn't 'sorted'? ...
>> other than as the order one wants?
> I want the order to be the same as the order I enter them in.  ;)

That's not the same thing at all.

I added Fred to the address book in January. I added Sue to the address book
in February. I added George to the address book in March. But I want them
to show up in the order Sue, George, Fred.

As far as I can see, the only way to display the contents of a dict in some
arbitrary order is to keep an auxilary list of the keys in the order you
want it.

“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

More information about the Python-list mailing list