[FRPythoneers] sorting a list of dictionaries

Andrew Diederich diederic at boulder.net
Mon Jan 20 17:22:26 MST 2003


I've made a python script to ldap query an exchange server, then kick out
a webpage of phone and email addresses.  The result set from the ldap
search is a list of two item tuples.  The first item in the tuple is the
DN (distinguished name) and the second is a dictionary of attributes like
uid, rfc822Mailbox, etc.

What I had done to alphabetize the list at first was to just sort the
initial result set, res.sort().  The problem with that is it basically
sorts usernames then, and it's simply not intuitive to see a page of
firstname, lastname sorted that way.  It's pretty close, but not quite.

To make the first list just a list of dictionaries I just
exchangelist = []
for x in res: exchangelist.append(x[1])

But I don't think exchangelist.sort() will sort by, say, cn (common name)
since I didn't tell it to.  And I'm not sure how.  So what I'd like to do
is either:
1) sort the list, then do a "for user in exhangelist: print ......." 
2) maybe pull out the values for the cn key into a list, then sort that,
and use that sorted list somehow to re-sort the exchangelist I have.

Does anyone have suggestions at good ways to do this?  If I've been
unclear, just let me know, and I can post more of what I've written.

Oh -- This is using python 1.5.2.  The linux box I'm running the script
for had ldap already compiled in, or maybe it was the only python module I
could find for ldap. I remember that ldap seemed under-represented in
python modules.

Thanks in advance,

Andrew Diederich
diederic at boulder.net

More information about the FRPythoneers mailing list