user17153595
user17153595

Reputation:

Add highest and lowest values from a list into separate lists without using max and min

Context: I'm learning python and I'd like to save the max and min numbers from a list without using the min and max functions from python. I was trying to do something like this:

inp_list = [1, 2, 3, 4, 78, 5, 7, 90]

res_max = [0]
res_min = [0]

for i in range(1,len(inp_list)):
    while i <= len(inp_list):
        if inp_list[i] > inp_list[i-1]: 
            res_max[0] = inp_list[i]
        break

for i in range(1, len(inp_list)):
    while i <= len(inp_list):
        if inp_list[i] < inp_list[i-1]:
            res_min[0] = inp_list[i]
        break

The res_max seems to be giving the correct number, but the res_min is not the minimum number from the generated list, but simply the latest minimum number before finding a larger number.

How could I do this with "vanilla" python as much as possible? Thank you in advance!

Upvotes: 0

Views: 792

Answers (4)

Olvin Roght
Olvin Roght

Reputation: 7812

This answer offers nothing new but performance improvement of balderman's answer. For some reasons he didn't react on comments.

Use slicing to skip first element is wrong way for many reasons. Generally it just slows down code execution and increases memory consumption.

One of alternative ways to skip first element could be working with iterator rather then list itself. We can get iterator using iter() and retrieve first value using next().

Code:

lst = [1, 2, 3, 4, 78, 5, 7, 90]
lst_iter = iter(lst)
_min = _max = next(lst_iter)
for num in lst_iter:
    if num < _min:
        _min = num
    if num > _max:
        _max = num
print('min:', _min, 'max:', _max)

Using iterator significantly reduces memory usage (proof) and increases execution speed (proof).

Upvotes: 0

balderman
balderman

Reputation: 23815

You can loop only once ...

lst = [1, 2, 3, 4, 78, 5, 7, 90]
# set min & max to point to the first element
_min = lst[0]
_max = lst[0]
for num in lst[1:]:
  if num < _min:
    _min = num
  if num > _max:
    _max = num
print(f'min: {_min}, max: {_max}')

output

min: 1, max: 90

Upvotes: 3

GaryMBloom
GaryMBloom

Reputation: 5690

Your logic is more complicated than it needs to be. I don't see any need to use a list for the maxes. But, to solve your current problem, just set the default values to the first entry in the inp_list. So...

res_max = [0]
res_min = [0]

Could be:

res_max = [inp_list[0]]
res_min = [inp_list[0]]

Looks to me on quick glance that you were setting the "min" to a value (0) that was already less than any value in the list to be checked. So, just grab the first element of the list for the default value.

To really simplify the program, I would go with something along these lines:

inp_list = [1, 2, 3, 4, 78, 5, 7, 90]

res_max = inp_list[0]
res_min = inp_list[0]

for i in inp_list:
    if i > res_max: 
        res_max = i
    if i < res_min:
        res_min = i

Upvotes: 0

ncica
ncica

Reputation: 7206

inp_list = [1, 2, 3, 4, 78, 5, 7, 90]

res_max = inp_list[0]
res_min = inp_list[0]

for i in inp_list:
    if i > res_max:
        res_max = i
    if i < res_min:
        res_min = i

print (res_max) # 90
print (res_min) # 1

Upvotes: 0

Related Questions