Implementing "select distinct ... from ..." over a list of Python dictionaries

Posted by daveslab on Stack Overflow See other posts from Stack Overflow or by daveslab
Published on 2010-05-05T17:48:16Z Indexed on 2010/05/05 17:58 UTC
Read the original article Hit count: 186

Hi folks,

Here is my problem: I have a list of Python dictionaries of identical form, that are meant to represent the rows of a table in a database, something like this:

[ {'ID': 1,
   'NAME': 'Joe',
   'CLASS': '8th',
   ... },
  {'ID': 1,
   'NAME': 'Joe',
   'CLASS': '11th',
   ... },
  ...]

I have already written a function to get the unique values for a particular field in this list of dictionaries, which was trivial. That function implements something like:

select distinct NAME from ...

However, I want to be able to get the list of multiple unique fields, similar to:

select distinct NAME, CLASS from ...

Which I am finding to be non-trivial. Is there an algorithm or Python included function to help me with this quandry?

Before you suggest loading the CSV files into a SQLite table or something similar, that is not an option for the environment I'm in, and trust me, that was my first thought.

© Stack Overflow or respective owner

Related posts about python

Related posts about database-queries