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,515
  • 11
  • 37
  • 59

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
  • 77,341
  • 8
  • 133
  • 139