This is a test version of Biostars. For the public version, visit https://www.biostars.org.
TypeError: must be real number, not dict in python

Hi, I am trying to use the log10() function in python on a set of numbers in a script. I am having this type of error "TypeError: must be real number, not dict" The list is done by numbers and string.

string python dict

1 answer

Is this what you are currently doing?

>>> import math
>>> d = {'a': 1, 'b': 10, 'c': 100}
>>> math.log10(d)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: must be real number, not dict

If you want to apply the log10 function to each element in the dictionary, try this instead:

>>> [math.log10(value) for key, value in d.items()]
[0.0, 1.0, 2.0]

@OP If you want to retrieve this info back still in dictionary form (but without updating/overwriting the existing dictionary), you can do it like so based on the code above:

new_d = {key: math.log10(value) for key, value in d.items()}
>>> {'a': 0.0, 'b': 1.0, 'c': 2.0}

Log in to answer this question.