0

I'm looking to get the maximum absolute value in a random list. I need to create it in a function. I'm not sure what I'm doing wrong here. I have a for loop setup so it should check each number but it's only returning the first value.

def main():
    print(maxabsinlst([-19, -3, 20, -1, 5, -25]))

def maxabsinlst(num):
    for x in (num):
        max_abs_value = num[5]
        if abs(x) > max_abs_value:
            max_abs_value = abs(x)
            return max(abs(max_abs_value))

main()
hrokr
  • 3,276
  • 3
  • 21
  • 39
Doug
  • 29
  • 2
  • It's not returning the first value. – superb rain Jul 31 '20 at 01:30
  • 2
    Welcome to Stack Overflow. Don't forget to accept an answer (tick the check-mark next to an answer) if it answers your question. In this way your question stops from show up as _unanswered_. Also up-vote good answers. – As you're starting out here, please take the [tour](https://stackoverflow.com/tour), read about [what's on-topic](https://stackoverflow.com/help/on-topic), and have a look at [How to Ask a Good Question](https://stackoverflow.com/help/how-to-ask). – Ivo Mori Jul 31 '20 at 02:14

5 Answers5

4

The shortest way would be:

def maxabsinlst(num):
    return max(map(abs, num))

The map() function applies abs() to each number on the list, and returns a list of these absolute values, then max() finds the maximum in the list.

Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
2

You are assigning max_abs_value to num[5] inside the loop. Instead assign it as first element of list then loop through it comparing with other elements

def main():
    print(maxabsinlst([-19, -3, 20, -1, 5, -25]))

def maxabsinlst(num):
    max_abs_value = abs(num[0])
    for x in num[1:]:
        if abs(x) > max_abs_value:
            max_abs_value = abs(x)
    return max_abs_value

main()

OR

In [36]: max(abs(i) for i in [-19, -3, 20, -1, 5, -25])
Out[36]: 25
bigbounty
  • 16,526
  • 5
  • 37
  • 65
2

You can make this quite a bit more concise:

def max_abs_in_lst(num):
    num = [abs(i) for i in num]
    return max(num)
hrokr
  • 3,276
  • 3
  • 21
  • 39
1

max_abs_value = num[5]

Every time the for loop runs, it is setting the max value to -25. Store the value outside the for loop and initialize it with max_abs_value = 0

RyanJMcGowan
  • 1,485
  • 1
  • 15
  • 33
1

There are a few issues with your function. First, you're setting max_abs_value at every iteration of the for loop. Second, you're assuming that the list passed to the function will have length 6 every time. Third, you never computed the absolute value of num[5]. So, you're comparing abs(x) to a negative number. Fourth, you're returning from the function as soon as you find any value greater than max_abs_value. You have no guarantee that it was the the greatest value in the list.

What you want is

def maxabsinlist(num):
    max_abs_value=abs(num[0])
    for x in num[1:]:
        value=abs(x)
        if value > max_abs_value:
            max_abs_value=value
    return max_abs_value
Daniel Walker
  • 6,380
  • 5
  • 22
  • 45