Increase value in hash table

Steven D'Aprano steve+comp.lang.python at pearwood.info
Wed Jan 23 05:46:50 EST 2013


On Wed, 23 Jan 2013 10:12:25 +0000, Oscar Benjamin wrote:

> You can't retrieve the value of printque[val] if you haven't yet added
> an entry with the key val to the dict. Try this:
> 
> if val not in printque:
>     printque[val] = 1
> else:
>     printque[val] = printque[val] + 1

Another way of writing that is:

printque[val] = printque.get(val, 0) + 1



-- 
Steven



More information about the Python-list mailing list