Django Get Second Last Element of List Example

Published On: 22/07/2022 | Category: Django Python

Hi Dev,

This article will give you example of django get second last element of list. let’s discuss about how to find second last element in list django. I would like to show you django get second item in list. In this article, we will implement a how to get second last element of list in python. So, let's follow few step to create example of django array get second last element.

In this example, I will create a simple list with the day with the name. Then I will get second last element with name using -2 key of array. so let's see the below example.

You can use these examples with python3 (Python 3) version.

let's see below simple example with output:

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

def index(request):

    myArray = ['Django', 'PHP', 'Html', 'CSS', 'Python']
  
    # Get Last Element
    lastElem = myList[-2]
      
    print(lastElem)
    
    return HttpResponse('')
Output
CSS

I Hope It will help you....