2

I have an array called list, for example

list = [0,1,2,3]

in the HTML when i do this

<body>
{{ list }}
</body>

the output will be [0,1,2]

what i want is

{{ list[0] }}

but this statement is giving me error, how i can just print the first element

6
  • 1
    {{ list[0] }} is neither HTML nor Python. What else are you using? Commented Mar 7, 2011 at 13:37
  • This is Django. See stackoverflow.com/questions/1700661/… Commented Mar 7, 2011 at 13:39
  • @msytNadeem [0,1,2] isn't an array, it's a list; array('l', [1, 2, 3, 4, 5]) IS an array. In Rome , do as the Romans do. Commented Mar 7, 2011 at 14:07
  • @eyquem A list is nothing but a sophisticated array I thought! But why that doesn't work? Is that a issue with templates? Commented Mar 7, 2011 at 19:15
  • @tamizhgeek I don't speak of an array in the vague sense of everyday usage, I speak of Python's types and of this one in particular: (docs.python.org/library/array.html#module-array) A Python's array can't be sophisticated to represent a list because "Arrays are sequence types and behave very much like lists, except that the type of objects stored in them is constrained." Commented Mar 7, 2011 at 20:23

1 Answer 1

6

If you're using Django, try this: {{ list.0 }} or for slicing: {{ list|slice:":1" }}

Docs: http://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#slice

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.