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

Chris Green cl at isbd.net
Wed Mar 8 14:27:17 EST 2017

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:-

    # Address Book field details, dictionary key is the database column 
    dbcol = {}
    dbcol['firstname'] = col('First Name', True, False)
    dbcol['lastname'] = col('Last Name', True, False)
    dbcol['email'] = col('E-Mail', True, True)
    dbcol['phone'] = col('Phone', True, True)
    dbcol['mobile'] = col('Mobile', True, True)
    dbcol['address'] = col('Address', True, False)
    dbcol['town'] = col('Town/City', True, False)
    dbcol['county'] = col('County/Region', True, False)
    dbcol['postcode'] = col('PostCode', True, False)
    dbcol['country'] = col('Country', True, False)
    dbcol['notes'] = col('Notes', True, False)
    dbcol['www'] = col('Web Page', True, True)
    dbcol['categories'] = col('Categories', True, True)

How can I get the fields in the GUI window in the order I want rather
than the fairly random order that they appear in at the moment?

Currently the GUI fields are populated by a for loop as follows:-

    # Put values into the fields 
    i = 0
    for col, field in abookdb.dbcol.items():
        if (i > numkeys/2):
            self.addfieldentry(col, address, field, self.rtable, i-numkeys/2)
            self.addfieldentry(col, address, field, self.ltable, i)
        i = i + 1

The for loop gets the items from the dictionary in an order that isn't
what I want.  How can I configure things so they're in the order I want?

Chris Green

More information about the Python-list mailing list