Here is an simple example that gets min, max, and avg values from a list.
The two functions below have same result.
I want to know the difference between these two functions.
And why use itertools.tee()
?
What advantage does it provide?
from statistics import median
from itertools import tee
purchases = [1, 2, 3, 4, 5]
def process_purchases(purchases):
min_, max_, avg = tee(purchases, 3)
return min(min_), max(max_), median(avg)
def _process_purchases(purchases):
return min(purchases), max(purchases), median(purchases)
def main():
stats = process_purchases(purchases=purchases)
print("Result:", stats)
stats = _process_purchases(purchases=purchases)
print("Result:", stats)
if __name__ == '__main__':
main()
_process_purchases(int(var) for var in ["1", "2", "3"])
– Aviatortee
in this case has not much interest though: asmin
will have to exhaust the iterator, all the values that it produced will have to be kept in memory formax
andmedian
. One could as well have created a list and runmin
andmax
on it. I just timed it, it's even a little bit faster. – Hassiehassin