'How to remove the list from a list of dictionaries

In Django, I do a 3rd party API call and get a list of dictionaries as a result. I would like to use this in my return render call in Django. But when I do this, I get:

'Context must be a dict rather than list'

How can I remove the list part and just keep the dictionary?

So for example, make:

[{'key1':'value1', 'key2': 'value2'}, {'key1':'value3', 'key2': 'value4'}]

a valid dictionary to use as context



Solution 1:[1]

you can put your list in a dictionary to use it as a context.

context = {
    "data": [
        {"key1": "value1", "key2": "value2"},
        {"key1": "value3", "key2": "value4"},
    ]
}
return render(request, template_name, context)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1