Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python return largest integer in the list [duplicate]

Tags:

python

list

I am new to python and am trying to work with lists. how can i get my function to accept a list of integers and then returns the largest integer in the list?

like image 213
user2627901 Avatar asked Jul 29 '13 04:07

user2627901


2 Answers

Use the built-in max function:

>>> L=[2,-7,3,3,6,2,5]
>>> max(L)
6

If you want to use max in a custom function, you could do this:

def getMaxOfList(L):
    return max(L)

I don't know why you would want to do this though, since it provides absolutely no new functionality

If you want to write your own implementation of max:

def myMax(L):
    answer = None
    for i in L:
        if i > answer:
            answer = i
    return answer
like image 84
inspectorG4dget Avatar answered Oct 15 '22 02:10

inspectorG4dget


use max function:

>>> L=[2,-7,3,3,6,2,5]
>>> L
[2, -7, 3, 3, 6, 2, 5]
>>> max(L)
6
like image 21
Mandar Pande Avatar answered Oct 15 '22 04:10

Mandar Pande