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

Tugrul Ates
  • 9,451
  • 2
  • 33
  • 59
msytNadeem
  • 173
  • 1
  • 4
  • 15
  • 1
    `{{ list[0] }}` is neither HTML nor Python. What else are you using? – Sven Marnach Mar 07 '11 at 13:37
  • This is Django. See http://stackoverflow.com/questions/1700661/access-array-elements-in-a-django-template – Tugrul Ates Mar 07 '11 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._ – eyquem Mar 07 '11 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? – tamizhgeek Mar 07 '11 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: (http://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."_ – eyquem Mar 07 '11 at 20:23
  • @eyquem Thanks! I learned a new thing – tamizhgeek Mar 08 '11 at 12:09

1 Answers1

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

Daniel Dinu
  • 1,783
  • 12
  • 16