How to Get the Last Element of a List in Django?
Published On: 22/07/2022 | Category:
Django
Python
Hi Dev,
In this tutorial, I will show you how to get last element of list in django. you will learn django get last element of list. you'll learn how to find last element of list in django. This tutorial will give you simple example of how to get last n elements of a list in python. Here, Creating a basic example of django get last element of list.
There are a few ways to get the last element from the list in django. You could use either by key with -1 or using pop function. so let's see the below examples:
You can use these examples with python3 (Python 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'] # Get Last Element lastElem = myList[-1] print(lastElem) return HttpResponse('')Output
PythonExample : 2 views.py
from django.shortcuts import render from django.http import HttpResponse def index(request): myArray = [11, 22, 33, 44, 55, 66] # Get Last Element lastElem = myList.pop() print(lastElem) return HttpResponse('')Output
66
I Hope It will help you....