0

In Python a way to calculate the max of a list and output a value even with an empty list is to do this:

max(python_list, default=0)

Is there any similar way (in one statement and obviously without try and except or if and else) in Python to calculate the average?

I used this simple code:

average = sum(python_list)/ len(python_list)

and obviously I get this error with an empty list:

ZeroDivisionError: division by zero
Outcast
  • 4,967
  • 5
  • 44
  • 99
  • What is the non-safe one? – Eugene Sh. Oct 15 '19 at 18:29
  • If the list is empty, both `max` and average are undefined. What did you want as a return value? Simple code around an empty list the same way you did for your "average". – Prune Oct 15 '19 at 18:30
  • @EugeneSh., by safe I mean just one which does not output an error when the list is empty but it outputs a specific value. – Outcast Oct 15 '19 at 18:30
  • @Prune, in one statement and without try and except? – Outcast Oct 15 '19 at 18:31
  • Yes -- apply the same principles. Post your code and the error you got. What do you *want* to return for numerator and denominator? – Prune Oct 15 '19 at 18:31
  • 3
    `sum(python_list) / len(python_list) if python_list else default` – Dani Mesejo Oct 15 '19 at 18:32
  • `average = np.mean(python_list)` – Quang Hoang Oct 15 '19 at 18:33
  • You can create a class that inherits from list and has a member method called `mean()` – Alex W Oct 15 '19 at 18:35
  • See my edited post @DanielMesejo - I obviously knew about if and else in the same statement/line but they are still if and else. – Outcast Oct 15 '19 at 18:37
  • 1
    How about `sum([x/len(lst) for x in lst])`? That has no conditions whatsoever. But has many divisions... – Eugene Sh. Oct 15 '19 at 18:40
  • `sum(lst) / (len(lst) or 1)` – Dani Mesejo Oct 15 '19 at 18:41
  • You could pretty easily write your own `average/mean` function that operates the same as `max` (with a default value), but why the restriction on `try/except` and `if/else`? – b_c Oct 15 '19 at 18:45
  • @DanielMesejo, if this is actually right (it works but I have not see it again) then you should post as an answer so it is also reviewed. – Outcast Oct 15 '19 at 18:46
  • As you can see in the link python core stats package raise an exception when the data list is empty. https://github.com/python/cpython/blob/f705f8e9b58955d0d9083e98d71ba01b2e69798c/Lib/statistics.py#L314 – balderman Oct 15 '19 at 18:54
  • [Catch the error](https://docs.python.org/3/tutorial/errors.html#handling-exceptions) and return a value in the except suite. – wwii Oct 15 '19 at 19:13

3 Answers3

2

Based on the behavior of the or operator on Truthy values, i.e. :

Return the first Truthy value if there are any, else return the last value in the expression.

You could do:

sum(lst) / (len(lst) or 1)

This will return 0 if the list lst is empty. Notice that is basically a shortcut for val if val else default.

Dani Mesejo
  • 61,499
  • 6
  • 49
  • 76
  • 1
    This may a concise solution for what I ask for (upvote). In any case, it seems that python has neither a function for the average nor for an average which returns a default error as the max or min function. – Outcast Oct 15 '19 at 18:56
  • @PoeteMaudit To the best of my knowledge there isn't one, at least in the standard library. – Dani Mesejo Oct 15 '19 at 18:58
  • *nor for an average which returns a default value in case of error as the max or min function. – Outcast Oct 15 '19 at 19:06
1

One way of doing this is:

sum([val/len(lst) for val in lst])

If the list is empty, nothing is divided by len(lst) (which is 0) so there are no errors. This will return 0 if the list is empty.

aaay aaay
  • 11
  • 3
0

Providing you consider that the answer is zero, I would do it this way, using the ternary operator:

avg = sum(lst) / len(lst) if lst else 0

That is a one-liner, at least.

fralau
  • 3,279
  • 3
  • 28
  • 41