Python Forum
Find the highest value of a list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Find the highest value of a list
#1
Hello,
I'm trying to build a small program to find the highest value of a list. I tried this :

def nb_max(L):
    maxi = L[0]
    for x in [0,len(L)-1]:
        if maxi<=L[x]:
               maxi=L[x]
        else :
            pass
    return maxi
But it doesn't work. I can't understand why....

Any idea ?

Thank you Smile
Reply
#2
When you write for x in [0, len(L)-1], x takes only two values: 0 and len(L)-1.

Also note that such a function already exists in Python, max(L) returns the highest value of L.
Reply
#3
(Oct-29-2021, 10:25 AM)Menthix Wrote: Hello,
I'm trying to build a small program to find the highest value of a list. I tried this :

def nb_max(L):
    maxi = L[0]
    for x in [0,len(L)-1]:
        if maxi<=L[x]:
               maxi=L[x]
        else :
            pass
    return maxi
But it doesn't work. I can't understand why....

Any idea ?

Thank you Smile

Hello,

I am only at page 30 of the Python book I am working through so I don't know what def and return do.
I rewrote your code using syntax I could understand and it seems to work OK:

[email protected]:~/Python$ cat prog22.py
L = [1, 2, 3]
maxi = L[0]
for x in [0,len(L)-1]:
if maxi<=L[x]:
maxi=L[x]
else :
pass
print (maxi)
[email protected]:~/Python$ python prog22.py
3
[email protected]:~/Python$

Are you sure your list was populated at the start?
Reply
#4
If you want to make your own max function rather than use the built-in max, for practice/homework/whatever.
Use a better name than L for the list using numbers seems more suitable.
Also you may as well name maxi maximum.
Iterate directly over the numbers in the for loop, as maximum has already been defined as the first item, the rest can be iterated by using:
for number in numbers[1:]:.
The if statement only needs to check if maximum is lower than number, not less than or equal to.
The else part of the if statement is not required
Reply
#5
(Oct-29-2021, 10:38 AM)Gribouillis Wrote: When you write for x in [0, len(L)-1], x takes only two values: 0 and len(L)-1.

Thank you, the issue definitly came from here !
Works better now.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Read directory listing of files and parse out the highest number? cubangt 5 125 Yesterday, 10:15 PM
Last Post: Larz60+
  read a text file, find all integers, append to list oldtrafford 12 746 Aug-11-2022, 08:23 AM
Last Post: Pedroski55
  find some word in text list file and a bit change to them RolanRoll 3 422 Jun-27-2022, 01:36 AM
Last Post: RolanRoll
  How to find the second lowest element in the list? Anonymous 3 451 May-31-2022, 01:58 PM
Last Post: Larz60+
  Python Program to Find the Total Sum of a Nested List vlearner 8 1,326 Jan-23-2022, 07:20 PM
Last Post: menator01
  Find Common Elements in 2 list quest 4 1,743 Apr-14-2021, 03:57 PM
Last Post: quest
  List of error codes to find (and count) in all files in a directory tester_V 8 2,285 Dec-11-2020, 07:07 PM
Last Post: tester_V
  Find _ in a list 1234 15 3,245 Dec-01-2020, 07:53 PM
Last Post: bowlofred
  Trying to find first 2 letter word in a list of words Oldman45 7 2,199 Aug-11-2020, 08:59 AM
Last Post: Oldman45
  How to find the first and last of one of several characters in a list of strings? tadsss 2 1,459 Jun-02-2020, 05:23 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020