It seems pretty basic to me, but I didn't found a good solution.
Assuming a dictionary like:
data = {'values' : [1.3333, None, 2.44444], 'other_values' : [2.3333, 1.2222, None]}
Because the built-in function round()
obviously can't round None
's, this returns an error:
for index in range(0, len(data['values'])):
result = round(data['values'][index], 2)
print(result)
result2 = round(data['other_values'][index], 2)
print(result2)
A possible solution is something like this
for index in range(0, len(data['values'])):
if data['values'][index]:
result = round(data['values'][index], 2)
print(result)
if data['other_values'][index]:
result2 = round(data['other_values'][index], 2)
print(result2)
but is there a more pythonic way?