Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

How to sum the values of a dict #192

Closed
charlax opened this Issue · 2 comments

3 participants

@charlax

Shouldn't sum be a function and not only a filter? How would you sum the values of a dict (e.g. a Counter instance)?

Those two (logically) do not work:

{{ number_of_stuff|sum(number_of_stuff.values()) }}
{{ number_of_stuff|sum(attribute=values) }}

Thanks!

@untitaker
Collaborator

number_of_stuff.values()|sum i think.

@mitsuhiko
Owner

This is possible in a way in 2.7 now:

>>> Template('{{ foo.items()|sum(attribute="1") }}').render(foo={'a': 1, 'b': 2, 'c': 3})
u'6'
@mitsuhiko mitsuhiko closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.