Equivalent of select distinct in sql

Is there a nice way to get all the distinct values from a particular column from a data table? Obviously I can do a search and loop through but I thought I’d check if there was a nicer way. Generic example

Book_id|Book name| Book colour|Publish date
1 |book1 | Blue | 1/2/17
2 |book2 | Red |30/12/16
3 |book3 | Blue |5/7/17

I’m after a list of colours e.g. [Blue, Red]. I may also want to perform a function on them before checking if distinct, for example to get the years books are published [2016, 2017].

Any thoughts?

1 Like

How about this?

unique_names = []
all_names = list(app_tables.tablewithnames.search())
for r in all_names:
    unique_names.append(r['Name'])
print set(unique_names)

Indeed, that is our recommended approach.

In fact, you can do that even more neatly with a comprehension:

unique_names = set((r['Name'] for r in app_tables.my_table.search()))
2 Likes

Ah, a true Python master :slight_smile: