Django print dictionary inside list in template


If you want to print dictionary inside list, here is the solution.

Imagine that, you have a list like this-

alldata =   [
                        { 
                            "tag" : "greeting",
                            "pattern" : ['hello', 'hi', 'how are you', 'how is the day'],
                            "answer" : ['Hi', 'Hello', 'Hey man', 'Whats up']
                        },
                        { 
                            "tag" : "goodbye",
                            "pattern" : ['Bye', 'See you later', 'Goodbye'],
                            "answer" : ['See you later', 'Thanks for your time', 'Have a nice day', 'Bye! Come back soon']
                        },
                        { 
                            "tag" : "thanks",
                            "pattern" : ['Thanks', 'Thank you', "That's fun"],
                            "answer" : ['Happy to chat', 'Thanks for your time', 'My pleasure']
                        }
                    ]

Now, if you want to print pattern from first dictionary from first list. So, in django template, just use like this-

{{ alldata.0.pattern }}

Printing first element from pattern-

{{ alldata.0.pattern.0 }}

In View.py

In view.py you are not able to do same thing. You need to change a bit, like this-

alldata[0]['pattern'][0]

Enjoy