5

let's say I have a list

li = [{'q':'apple','code':'2B'},
      {'q':'orange','code':'2A'},
      {'q':'plum','code':'2A'}]

What is the most efficient way to return the count of unique "codes" in this list? In this case, the unique codes is 2, because only 2B and 2A are unique.

I could put everything in a list and compare, but is this really efficient?

Eli Courtwright
  • 186,300
  • 67
  • 213
  • 256
TIMEX
  • 259,804
  • 351
  • 777
  • 1,080
  • 2
    Duplicate of all of these: http://stackoverflow.com/search?q=%5Bpython%5D+duplicate+list. Specifically this: http://stackoverflow.com/questions/1143379/removing-duplicates-from-list-of-lists-in-python – S.Lott Apr 03 '10 at 02:40

1 Answers1

8

Probably the most efficient simple way is to create a set of the codes, which will filter out uniques, then get the number of elements in that set:

count = len(set(d["code"] for d in li))

As always, I advise to not worry about this kind of efficiency unless you've measured your performance and seen that it's a problem. I usually think only about code clarity when writing this kind of code, and then come back and tighten it only if I've profiled and found that I need to make performance improvements.

Eli Courtwright
  • 186,300
  • 67
  • 213
  • 256
  • 1
    FWIW when sets were first introduced in Python this was a novel concept to me. I found it very useful to read up on the math behind sets, which ties it all together: http://en.wikipedia.org/wiki/Set_(mathematics) – jathanism Apr 02 '10 at 23:42
  • 2
    To take the unique of some *group*, it's always O(n), since you have look at every element, unless you have some other inside information. – Gregg Lind Apr 03 '10 at 00:11