I have a dict from a json API call that looks like this:
{
'id': '63d08d5c57abd98fdeea7985',
'pageName': 'Some Page Name',
'bodyContent':
[{
'children': [{
'alpha': 'foo foo foo',
'beta': 'bar bar bar'
}]
}],
'date': '2023-01-25T02:01:00.965Z'
}
To reference the nested items inside bodyContent, I'm doing a loop within a loop (to get alpha):
{% for item in items.get('bodyContent') %}
{% for i in item.get('children') %}
{{ i['alpha'] }}
{% endfor %}
{% endfor %}
I see these nested for loops in a lot of example code, but is this really the best way - a loop within a loop? I can't help but feel like it's a bit dirty and am used to other languages where a loop within a loop isn't necessary for a structure that is this basic.
Edit: What other languages am I referring to, where nested loops wouldn't be necessary? Things like array_map or array_reduce in PHP to simplify data structures (when you can't alter how they're stored, like the data in my example from an API). If nested loops are A-OK in Python then that's cool too. I'm just not sure what the common wisdom is with them in Python.