0

I have a list of dictionaries like:

list = [{'name':'Mike', 'sex':'m'}, {'name':'Rose', 'sex':'f'}]

And I need to count how many dictionaries with sex = f are in the list. I've tried something like:

count = (p['sex'] == 'f' for p in list) 

but count returns as <generator object <genexpr> at 0x1068831e0> which I don't know what is.

Hyperion
  • 2,355
  • 11
  • 34
  • 56

1 Answers1

1

Counts are not done implicitly, you have to work that out explicitly by for example using the builtin sum:

count = sum(p['sex'] == 'f' for p in list) 

You can read up on generator expression from the docs:

Generator expressions

Moses Koledoye
  • 74,909
  • 8
  • 119
  • 129