How to Remove Last Element from List in Django?

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

Hi Dev,

Today, I would like to show you django list delete last element. you can understand a concept of django list remove last element. I explained simply about django list remove one element. it's simple example of python list remove last n elements.

In this example, I will give you two examples. one using "del" in django and another using "pop" function of array. we will pass last key and it will remove last element from array in django. so let's see below examples.

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']
  
    # Remove Last Element
    myArray.pop()
      
    print(myArray)
    
    return HttpResponse('')
Output
['Django', 'PHP', 'Html', 'CSS']
Example : 2 views.py
from django.shortcuts import render
from django.http import HttpResponse
import array as arrayLib

def index(request):

    myArray = [11, 22, 33, 44, 55, 66]
   
    # Remove Last Element
    del myArray[-1]
       
    print(myArray)
    
    return HttpResponse('')
Output
[11, 22, 33, 44, 55]

I Hope It will help you....