Slicing a list in Django template

Each Answer to this Q is separated by one/two green lines.

If I want to get first 5 elements from a list I would do mylist|slice:"5"

but I want a range, let say from 3 to 7. something like mylist[3:8] how would I do that in template

you can just use

{{ mylist|slice:"3:8" }}

Its simple you will have to pass this in the slice filter then:

{{ mylist|slice:"3:8" }}

This filter takes care of all type of slicing you can perform on a list

e.g. All this would work:

{{ mylist|slice:"3:8" }}

{{ mylist|slice:":2" }}

{{ mylist|slice:"3:" }}

{{ mylist|slice:":" }}


The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .