How to Get Max Value from Django List?

Published On: 13/08/2022 | Category: Django


Hi Dev,

This post will give you example of django get max value from list. This article will give you simple example of django list find max value. if you want to see example of django list get max value index example then you are a right place. We will look at example of django find max value in list of objects. Let's see bellow example how to get max value from django list.

There are a few ways to get the largest number from the list in django. i will give you two examples using for loop with max() to get max number from list. so let's see the below examples.

You can use these examples with django3 (Django 3) version.

let's see below a simple example with output:

Example : 1 views.py
from django.shortcuts import render
from django.http import HttpResponse

def index(request):

    myList = [10, 100, 20, 200, 50]
  
    # Get Max number from List
    maxValue = max(myList)
      
    print(maxValue)
    
    return HttpResponse(maxValue)
Output
200
Example : 2 views.py
from django.shortcuts import render
from django.http import HttpResponse

def index(request):
    
    myList = [10, 100, 20, 200, 50]
  
    # Get Max number from List
    myList.sort()
    maxValue = myList[-1]
      
    print(maxValue)
    
    return HttpResponse(maxValue)
Output
200

I Hope It will help you....