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:

andrew@ASUS-Laptop:~/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)
andrew@ASUS-Laptop:~/Python$ python prog22.py
3
andrew@ASUS-Laptop:~/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
  Program to find Mode of a list PythonBoy 6 997 Sep-12-2023, 09:31 AM
Last Post: PythonBoy
  find random numbers that are = to the first 2 number of a list. Frankduc 23 3,013 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  Find (each) element from a list in a file tester_V 3 1,155 Nov-15-2022, 08:40 PM
Last Post: tester_V
  Read directory listing of files and parse out the highest number? cubangt 5 2,252 Sep-28-2022, 10:15 PM
Last Post: Larz60+
  read a text file, find all integers, append to list oldtrafford 12 3,371 Aug-11-2022, 08:23 AM
Last Post: Pedroski55
  find some word in text list file and a bit change to them RolanRoll 3 1,482 Jun-27-2022, 01:36 AM
Last Post: RolanRoll
  How to find the second lowest element in the list? Anonymous 3 1,905 May-31-2022, 01:58 PM
Last Post: Larz60+
  Python Program to Find the Total Sum of a Nested List vlearner 8 4,787 Jan-23-2022, 07:20 PM
Last Post: menator01
  Find Common Elements in 2 list quest 4 2,685 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 3,584 Dec-11-2020, 07:07 PM
Last Post: tester_V

Forum Jump:

User Panel Messages

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