How to print defaultdict variable without its type?

user6867490 picture user6867490 · Dec 24, 2016 · Viewed 13k times · Source

In the following code:

from collections import defaultdict

confusion_proba_dict = defaultdict(float)

for i in xrange(10):
    confusion_proba_dict[i] = i + 10

print confusion_proba_dict

Output is:

defaultdict(<type 'float'>, {0: 10, 1: 11, 2: 12, 3: 13, 4: 14, 5: 15, 6: 16, 7: 17, 8: 18, 9: 19})

But, I need output to be:

{0: 10, 1: 11, 2: 12, 3: 13, 4: 14, 5: 15, 6: 16, 7: 17, 8: 18, 9: 19}

How can I do it?

Answer

Copperfield picture Copperfield · Dec 24, 2016

just transform it to a regular dict

print( dict(confusion_proba_dict) )

but if you are going to use like that, just use a regular dict as you don't use any of the advantages of defaultdict