﻿ Python: Generate the running maximum, minimum value of the elements of an iterable - w3resource

# Python: Generate the running maximum, minimum value of the elements of an iterable

## Python Itertools: Exercise-3 with Solution

Write a Python program to generate the maximum and minimum values of the elements of an iterable.

Sample Solution:

Python Code:

``````from itertools import accumulate
def running_max_product(iters):
return accumulate(iters, max)
#List
result = running_max_product([1,3,2,7,9,8,10,11,12,14,11,12,7])
print("Running maximum value of a list:")
for i in result:
print(i)
#Tuple
result = running_max_product((1,3,3,7,9,8,10,9,8,14,11,15,7))
print("Running maximum value of a Tuple:")
for i in result:
print(i)
def running_min_product(iters):
return accumulate(iters, min)
#List
result = running_min_product([3,2,7,9,8,10,11,12,1,14,11,12,7])
print("Running minimum value of a list:")
for i in result:
print(i)
#Tuple
result = running_min_product((1,3,3,7,9,8,10,9,8,0,11,15,7))
print("Running minimum value of a Tuple:")
for i in result:
print(i)
```
```

Sample Output:

```Running maximum value of a list:
1
3
3
7
9
9
10
11
12
14
14
14
14
Running maximum value of a Tuple:
1
3
3
7
9
9
10
10
10
14
14
15
15
Running minimum value of a list:
3
2
2
2
2
2
2
2
1
1
1
1
1
Running minimum value of a Tuple:
1
1
1
1
1
1
1
1
1
0
0
0
0
```

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

﻿

## Python: Tips of the Day

Inverts a dictionary with non-unique hashable values:

Example:

```def tips_collect_dictionary(obj):
inv_obj = {}
for key, value in obj.items():
inv_obj.setdefault(value, list()).append(key)
return inv_obj
ages = {
"Owen": 25,
"Jhon": 25,
"Pepe": 15,
}
print(tips_collect_dictionary(ages))
```

Output:

```{25: ['Owen', 'Jhon'], 15: ['Pepe']}
```

We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook