In python 3.x keys()
, values()
and items()
return views. Now while views certainly have advantages, they also seem to cause some compatibility issues. For example with matplotlib
(ultimately it's with numpy
). As an example this and this answers on stackexchange questions work just fine with python 2.x but raise an Exception when executing them in python 3.4.
A minimal example would be:
import matplotlib.pyplot as plt
d = {1: 2, 2: 10}
plt.scatter(d.keys(), d.values())
Which raises TypeError: float() argument must be a string or a number, not 'dict_values'
with python 3.4.
While for the minimal example the Exception is quite clear, this question arises because of the same problem and here the Exception is a lot less clear: TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
What is the best practice to deal with this issue? Can we hope that in a new release of matplotlib
(or ultimately numpy
) this issue will be dealt with or should we just start to write things like list(dict.values())
when using matplotlib
just to be sure not to run into trouble with python 3.x?