Python - Find the greatest number in a list of numbers

0 votes
asked Jun 22, 2010 by chris-foster

Is there any easy way or function to determine the greatest number in a python list? I could just code it, as I only have three numbers, however it would make the code a lot less redundant if I could tell the greatest with a built in function or something.

6 Answers

0 votes
answered Jun 22, 2010 by jubal

What about max()

foo = [1,2,3]
print max(foo)
0 votes
answered Jun 22, 2010 by chetan

Use max()

>>> l = [1, 2, 5]
>>> max(l)
5
>>> 
0 votes
answered Jun 22, 2010 by cletus

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.

0 votes
answered Jun 7, 2015 by user3483558
    #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)
0 votes
answered Sep 15, 2017 by chetan-vasudevan

If you have to find it without using max function then you can follow the code below:

    a=[1,2,3,4,6,7,99,88,999]
    max= 0
    for i in a:
        if i > max:
            max=i
    print(max)
0 votes
answered Sep 15, 2017 by sanjay-idpuganti

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 
Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...