How to Remove First Element from List in Django?

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

Hi Dev,

This article will provide example of django list delete first element. Here you will learn django list remove first element. we will help you to give example of python list remove one element. I would like to share with you django list remove first n elements. Alright, let’s dive into the steps.

In this article i will give you two examples. one using "del" in python and another using "pop" function of array. we will pass first key and it will remove first element from list in django. so let's see below examples.

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

Let's see bellow example here you will learn python django delete first element.

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

def index(request):

    myArray = ['Python', 'Django', 'PHP', 'HTML', 'CSS']
  
    # Remove First Element
    myArray.pop(0)
  
    print(myArray)
    
    return HttpResponse('')
Output
['Django', 'PHP', 'HTML', 'CSS']
Example : 2 views.py
from django.shortcuts import render
from django.http import HttpResponse

def index(request):

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

I Hope It will help you....