How to Get First Element of List in Django?
Published On: 21/07/2022 | Category:
Django
Python
Hi Dev,
I am going to explain you example of django get first element from list. I explained simply step by step django get first element of list of lists. if you have question about how to get first element of list in django then I will give simple example with solution. you can see how to get first n elements of a list in python. Let's get started with django first element of list.
In this tuts, there are a few ways to get the first element from the list in django. You could use either return only value in string or return array of the first value. 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.pyfrom django.shortcuts import render from django.http import HttpResponse def index(request): myArray = ['Django', 'PHP', 'Html', 'CSS', 'Python'] # Get First Element firstElem = myList[0] print(firstElem) return HttpResponse('')Output
DjangoExample : 2 views.py
from django.shortcuts import render from django.http import HttpResponse def index(request): myArray = [11, 22, 33, 44, 55, 66] # Get First Element firstElem = myList[:1] print(firstElem) return HttpResponse('')Output
[11]
I Hope It will help you....