How to Append Values to Python List in Django?

Published On: 05/08/2022 | Category: Django


Hi Dev,

Now, let's see example of django list append example. We will use django list append django basic commands. you will learn python list append django view-list append. let’s discuss about python append multiple items to list. Follow bellow tutorial step of django append to arrayfield.

In this article i will give you two examples. one using "append" function of array and another using function of array inside a nested array list. you need to pass value and it will append element from django array. so let's see below examples.

You can use these examples with django3 (Django 3) version.

let's see bellow example here you will learn python django list append django view-string append.

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

def index(request):

    aList = ['Html', 'Python', 'PHP'];
    aList.append( 'Django' );
    print(aList)
    
    return HttpResponse(aList)
Output
['Html', 'Python', 'PHP', 'Django']
Example : 2 views.py
from django.shortcuts import render
from django.http import HttpResponse

def index(request):
    
    my_list = ['tuts','station']
    another_list = [1, 2, 3, 4]
    my_list.append(another_list)
    print(my_list)
    
    return HttpResponse(my_list)
Output
['tuts', 'station', [1, 2, 3, 4]]

I Hope It will help you....