Django List Remove Element by Value Example

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

Hi Dev,

I am going to explain you example of django list remove element by value. this example will help you django list remove element example. you'll learn django list delete element value. you can see python list remove element by value. Let's see bellow example how to remove array element by value in django.

In this article i will give you two examples. one using "remove" function of array and another using "discard" function of array module. you need to pass value and it will remove element from django list. 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 list remove element by value.

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

def index(request):

    # Create Array
    myArrayObj = arrayLib.array('i', [1, 2, 3, 4, 5, 6])
    myArray = set(myArrayObj)
  
    # Remove Element
    myArray.discard(3)
  
    print(myArray)
    
    return HttpResponse('')
Output
{1, 2, 4, 5, 6}

I Hope It will help you....