0

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?

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
  • Does this answer your question? [How to check if object is not None within a list comprehension?](https://stackoverflow.com/questions/44807107/how-to-check-if-object-is-not-none-within-a-list-comprehension) – Gino Mempin Aug 12 '20 at 23:51
  • In some cases it is better to use `math.nan` rather than `None` to represent invalid values. The former can undergo many mathematical operations. – GZ0 Aug 13 '20 at 05:38

4 Answers4

2

Instead of round(x, 2) you could do round(x or 0, 2).

superb rain
  • 5,300
  • 2
  • 11
  • 25
  • This is clever but changes the result. Instead of `None` being preserved, you get 0. Also, it doesn't really help with OP's unpythonic `range` loop and `if` statements. – ggorlen Aug 12 '20 at 16:13
  • @ggorlen Yeah, but even their own "possible solution" changes the result (in a different way - zeros aren't printed anymore), so I figured this might work as well. Can't tell if they don't say what exactly they want and only show two disagreeing codes. – superb rain Aug 12 '20 at 16:19
  • Good point, I missed that they're not even mutating their structure. I guess the question is unclear since all they're doing is printing. – ggorlen Aug 12 '20 at 16:23
2

Maybe something like this:

data = {'values' : [1.3333, None, 2.44444], 'other_values' : [2.3333, 1.2222, None]}
lst = []
for key in data:
    lst.extend([round(num,2) for num in data[key] if num])
print(lst)

Output:

[1.33, 2.44, 2.33, 1.22]

Hope this helped!

1

I am not quite sure what you expect from the output. When you have the given values and other_values as keys, and just want to simply print out the round numbers, and ignoring None, maybe you can try this:

print([round(i, 2) for i in data['values'] if i is not None]) # so you still have 0 if there was any 0 in the data
print([round(i, 2) for i in data['other_values'] if i is not None])
Ming
  • 81
  • 5
0

You can use isinstance attribute

Like: if data['values'][0] is an attribute of float, then print

if isinstance(data['values'][index], float):

Full code:

if __name__ == '__main__':
    data = {'values': [1.3333, None, 2.44444],
            'other_values': [2.3333, 1.2222, None]}

    for index in range(0, len(data['values'])):
        if isinstance(data['values'][index], float):
            result = round(data['values'][index], 2)
            print(result)
        if isinstance(data['other_values'][index], float):
            result2 = round(data['other_values'][index], 2)
            print(result2)

Output:

1.33
2.33
1.22
2.44
Ahmet
  • 7,527
  • 3
  • 23
  • 47