Django Remove Empty String from List Example

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

Hi Dev,

This is a short guide on django remove empty value from list. We will look at example of django remove empty string from list. This post will give you simple example of how to remove empty string from list in django. This tutorial will give you simple example of how to remove empty string in python. Here, Creating a basic example of django remove empty strings in a list..

In this example, There are several ways to remove empty string values from the list in django. we will use filter(), join() and remove() functions to delete empty string from list. 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.py
from django.shortcuts import render
from django.http import HttpResponse

def index(request):

    myList = ['', 'Tuts-Station.com', '', 'is', 'Best', '']
  
    # Remove Empty Value from List
    newList = list(filter(None, myList))
  
    print(newList)
    
    return HttpResponse('')
Output
['Tuts-Station.com', 'is', 'Best']
Example : 2 views.py
from django.shortcuts import render
from django.http import HttpResponse

def index(request):

    myList = ['', 'Tuts-Station.com', '', 'is', 'Best', '']
  
    # Remove Empty Value from List
    newList = ' '.join(myList).split()
      
    print(newList)
    
    return HttpResponse('')
Output
['Tuts-Station.com', 'is', 'Best']

I Hope It will help you....