Django List Print All Elements Except First Example

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


Hi Dev,

If you need to see example of django list get all elements except first. you can see django list get all but not first element. I explained simply about django all items in list except first. In this article, we will implement a python all elements except first.

There are many ways to get all elements except the first element in list django. i will give you two examples using for loop with list[1:] to except first element from list. so let's see the below examples.

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

let's see below a simple example with output:

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

def index(request):

    myList = [1, 2, 3, 4, 5]
  
    # Get All Value Except First One
    myList = myList[1:]
      
    print(myList)
    
    return HttpResponse(myList)
Output
[2, 3, 4, 5]
Example : 2 views.py
from django.shortcuts import render
from django.http import HttpResponse

def index(request):
    
    myList = ["One", "Two", "Three", "Four", "Five"]
  
    # Get All Value Except First One
    for listElem in myList[1:]:
        print(listElem)

    return HttpResponse(listElem)
Output
Two
Three
Four
Five

I Hope It will help you....