[Solved] What does list comprehension “counts[num]=counts.get(num,0)+1” mean? [closed]


List Comprehension

This is not list comprehension.

List comprehesion for example:

lst2 = [x for x in lst1 if x % 2 == 0]

Where lst1 is a list and we filter in only values that are even if x % 2 == 0 and assign it to list lst2.

Meaning of counts[num]=counts.get(num,0)+1

  1. counts.get(num,0) get value of num key from counts dictionary, if key not exists return 0
  2. So counts[num]=counts.get(num,0)+1 would result in increasing counts[num] by one (and if not exists would by set to one)

3

solved What does list comprehension “counts[num]=counts.get(num,0)+1” mean? [closed]