106

How can I easily find the greatest number in a given list of numbers?


See also How do I find the maximum (larger, greater) of 2 numbers? - in that special case, the two values can also be compared directly.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Chris Foster
  • 2,639
  • 2
  • 23
  • 30

7 Answers7

157

What about max()

highest = max(1, 2, 3)  # or max([1, 2, 3]) for lists
Neuron
  • 5,141
  • 5
  • 38
  • 59
Jubal
  • 8,357
  • 5
  • 29
  • 30
  • Here's another that is also useful: [find out which variable contains the greatest number](https://stackoverflow.com/q/23154821/1447509) – cssyphus Sep 12 '18 at 23:38
18

You can use the inbuilt function max() with multiple arguments:

print max(1, 2, 3)

or a list:

list = [1, 2, 3]
print max(list)

or in fact anything iterable.

cletus
  • 616,129
  • 168
  • 910
  • 942
  • 6
    It's not good to name variables a default builtin python keyword, that overwrites the actual keyword, (you must know that) :-) – U13-Forward Oct 19 '18 at 01:16
18

This approach is without using max() function

a = [1,2,3,4,6,7,99,88,999]
max_num = 0
for i in a:
    if i > max_num:
        max_num = i
print(max_num)

Also if you want to find the index of the resulting max,

print(a.index(max_num))

Direct approach by using function max()

max() function returns the item with the highest value, or the item with the highest value in an iterable

Example: when you have to find max on integers/numbers

a = (1, 5, 3, 9)
print(max(a))
>> 9

Example: when you have string

x = max("Mike", "John", "Vicky")
print(x)
>> Vicky

It basically returns the name with the highest value, ordered alphabetically.

Chetan_Vasudevan
  • 2,414
  • 1
  • 13
  • 34
11

Use max()

>>> l = [1, 2, 5]
>>> max(l)
5
>>> 
Chetan
  • 46,743
  • 31
  • 106
  • 145
2

max is a builtin function in python, which is used to get max value from a sequence, i.e (list, tuple, set, etc..)

print(max([9, 7, 12, 5]))

# prints 12 
Sanjay Idpuganti
  • 312
  • 5
  • 11
2

You can actually sort it:

sorted(l,reverse=True)

l = [1, 2, 3]
sort=sorted(l,reverse=True)
print(sort)

You get:

[3,2,1]

But still if want to get the max do:

print(sort[0])

You get:

3

if second max:

print(sort[1])

and so on...

U13-Forward
  • 69,221
  • 14
  • 89
  • 114
  • 1
    Sorting a list is a more complex operation O(n log n) than just getting the maximum value O(n) – Allan Jun 27 '19 at 08:44
-7
    #Ask for number input
first = int(raw_input('Please type a number: '))
second = int(raw_input('Please type a number: '))
third = int(raw_input('Please type a number: '))
fourth = int(raw_input('Please type a number: '))
fifth = int(raw_input('Please type a number: '))
sixth = int(raw_input('Please type a number: '))
seventh = int(raw_input('Please type a number: '))
eighth = int(raw_input('Please type a number: '))
ninth = int(raw_input('Please type a number: '))
tenth = int(raw_input('Please type a number: '))

    #create a list for variables
sorted_list = [first, second, third, fourth, fifth, sixth, seventh, 
              eighth, ninth, tenth]
odd_numbers = []

    #filter list and add odd numbers to new list
for value in sorted_list:
    if value%2 != 0:
        odd_numbers.append(value)
print 'The greatest odd number you typed was:', max(odd_numbers)
Shalbert
  • 1,044
  • 11
  • 17
  • 4
    1) there's no reason not to do my_list = sorted([int(raw_input('Please type a number')) for _ in xrange(10)) versus typing extra stuff. 2) you have a list called sorted_list but you don't actually sort it 3) There's nothing in the question asking about filtering out odd numbers only 4) What does this provide that the previous answers 5 year ago didn't (besides answering a question that wasn't asked, and in a less elegant way) – Foon Oct 07 '15 at 16:06