How to Get Unique Elements from List in Django?
Published On: 25/07/2022 | Category:
Django
Python
Hi Dev,
Today, django list get unique elements is our main topic. This article goes in detailed on how to find unique values in django list. This article goes in detailed on how to get unique elements from list in django. you can see how to find unique elements in list python. Let's see bellow example python all unique elements in list.
There are several ways to get unique values from the list in django. we will use set() and dict() functions to get unique elements from list. so let's see the below examples.
You can use these examples with django3 (django 3) version.
let's see below simple example with output:
Example : 1 views.pyfrom django.shortcuts import render from django.http import HttpResponse def index(request): myArray = ['Django', 'PHP', 'Html', 'CSS', 'Python', 'Html', 'Django'] # Remove Duplicate Value from List newList = list(set(myList)) print(newList) return HttpResponse('')Output
['Django', 'PHP', 'Html', 'CSS', 'Python']Example : 2 views.py
from django.shortcuts import render from django.http import HttpResponse def index(request): myArray = [11, 22, 33, 44, 55, 66, 22, 33, 44] # Remove Duplicate Value from List newList = list(dict.fromkeys(myList)) print(newList) return HttpResponse('')Output
[11, 22, 33, 44, 55, 66]
I Hope It will help you....