Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initializing a dictionary with zeroes [duplicate]

I am trying to track seen elements, from a big array, using a dict. Is there a way to force a dictionary object to be integer type and set to zero by default upon initialization?

I have done this with a very clunky codes and two loops.

Here is what I do now:

fl = [0, 1, 1, 2, 1, 3, 4]
seenit = {}

for val in fl:
    seenit[val] = 0

for val in fl:
    seenit[val] = seenit[val] + 1
like image 231
giggle usa Avatar asked Dec 01 '25 04:12

giggle usa


1 Answers

Of course, just use collections.defaultdict([default_factory[, ...]]):

from collections import defaultdict

fl = [0, 1, 1, 2, 1, 3, 4]

seenit = defaultdict(int)

for val in fl:
    seenit[val] += 1

print(fl)
# Output
defaultdict(<class 'int'>, {0: 1, 1: 3, 2: 1, 3: 1, 4: 1})

print(dict(seenit))
# Output
{0: 1, 1: 3, 2: 1, 3: 1, 4: 1}

In addition, if you don't like to import collections you can use dict.get(key[, default])

fl = [0, 1, 1, 2, 1, 3, 4]

seenit = {}

for val in fl:
    seenit[val] = seenit.get(val, 0) + 1

print(seenit)
# Output
{0: 1, 1: 3, 2: 1, 3: 1, 4: 1}

Also, if you only want to solve the problem and don't mind to use exactly dictionaries you may use collection.counter([iterable-or-mapping]):

from collections import Counter

fl = [0, 1, 1, 2, 1, 3, 4]

seenit = Counter(f)

print(seenit)
# Output
Counter({1: 3, 0: 1, 2: 1, 3: 1, 4: 1})

print(dict(seenit))
# Output
{0: 1, 1: 3, 2: 1, 3: 1, 4: 1}

Both collection.defaultdict and collection.Counter can be read as dictionary[key] and supports the usage of .keys(), .values(), .items(), etc. Basically they are a subclass of a common dictionary.

If you want to talk about performance I checked with timeit.timeit() the creation of the dictionary and the loop for a million of executions:

  • collection.defaultdic: 2.160868141 seconds
  • dict.get: 1.3540439499999999 seconds
  • collection.Counter: 4.700308418999999 seconds

collection.Counter may be easier, but much slower.

like image 157
Ender Look Avatar answered Dec 02 '25 17:12

Ender Look