simultaneous assignment

Boris Borcic bborcic at
Wed May 10 11:48:13 CEST 2006

Steve R. Hastings wrote:

> You could also use a function that counts all different values in a list,
> reducing the list to a dictionary whose keys are the unique values from
> the list.  I got the idea from a discussion here on comp.lang.python; I
> called my version of it tally().
> d = tally(bool(x) for x in seq)
> print d[True]  # prints how many true values in seq
> print d[False]  # prints how many false values in seq
> tally() is in my module, which you can get here:

 >>> from itertools import groupby
 >>> tally = lambda it : dict((x,sum(1 for _ in y)) for x,y in groupby(sorted(it)))
 >>> tally('abbcabbcca')
{'a': 3, 'c': 3, 'b': 4}

More information about the Python-list mailing list