Active March 27, 2019    /    Viewed 84539    /    Comments 0    /    Edit

To find the largest number in a list with python, a solution is to use the function max():

````>>> l = [4,7,9,4,1,6,9]`
`>>> max(l)`
`9`
```

which returns 9 here. To find the index, there is the function index(), example:

````>>> l.index(max(l))`
`2`
```

Note: this function only returns the first index found. To find all the indexes of a maximum value (if there is several occurrences of the maximum value), we can do like in this example:

````>>> indices = [i for i, x in enumerate(l) if x == max(l)]`
`>>> indices`
`[2, 6]`
```

To find if there is several times the max value in the list, a solution is to use the python function count(), example:

````>>> l = [4,7,9,4,1,6,9]`
`>>> max_value = max(l)`
`>>> max_value `
`9`
`>>> l.count(max_value)`
`2`
```

it is then possible to write a simple function that returns the index(es):

````>>> def get_indexes_max_value(l):`
`...     max_value = max(l)`
`...     if l.count(max_value) > 1:`
`...             return [i for i, x in enumerate(l) if x == max(l)]`
`...     else:`
`...             return l.index(max(l))`
`... `
`>>> get_indexes_max_value(l)`
`[2, 6]`
```

### References

Liens Site
Built-in Functions: max() Python Doc
Python List max() Method Tutorial Point
How to find positions of the list maximum? stackoverflow
Python Finding Index of Maximum in List stackoverflow
How to return the maximum element of a list in Python? stackoverflow
Python: Getting the max value of y from a list of objects stackoverflow
Pythonic way to find maximum value and its index in a list? stack overflow
Python's most efficient way to choose longest string in list? stack overflow
How does Python compare string and int? stack overflow

##### Daidalos

Hi, I am Ben.

I have developed this web site from scratch with Django to share with everyone my notes. If you have any ideas or suggestions to improve the site, let me know ! (you can contact me using the form in the welcome page). Thanks!