Find the greatest number in a list of numbers

PythonNumbersMax

Python Problem Overview


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.

Python Solutions


Solution 1 - Python

What about max()

highest = max(1, 2, 3)  # or max([1, 2, 3]) for lists

Solution 2 - Python

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.

Solution 3 - Python

> 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.

Solution 4 - Python

Use max()

>>> l = [1, 2, 5]
>>> max(l)
5
>>> 

Solution 5 - Python

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 

Solution 6 - Python

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...

Solution 7 - Python

    #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)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChris FosterView Question on Stackoverflow
Solution 1 - PythonJubalView Answer on Stackoverflow
Solution 2 - PythoncletusView Answer on Stackoverflow
Solution 3 - PythonChetan_VasudevanView Answer on Stackoverflow
Solution 4 - PythonChetanView Answer on Stackoverflow
Solution 5 - PythonSanjay IdpugantiView Answer on Stackoverflow
Solution 6 - PythonU12-ForwardView Answer on Stackoverflow
Solution 7 - PythonShalbertView Answer on Stackoverflow