Chris_Carr | 2022-03-26 20:54:31 UTC | #1
I am trying to loop through a nested dictionary and print the key and value for every item in the dictionary. How may I achieve this?
Example Dictionary:
{'conversation_end': '2021-11-03T13:44:10.896000+00:00',
'conversation_id': '560904d6-5997-4ae2-a234-gfgfgfgf',
'conversation_initiator': None,
'conversation_start': '2021-11-03T13:41:22.603000+00:00',
'division_ids': ['7ef8bfd4-e12e-4310-90a1-fafg'],
'evaluations': None,
'external_tag': None,
'knowledge_base_ids': None,
'media_stats_min_conversation_mos': 4.872452259063721,
'media_stats_min_conversation_r_factor': 92.01608276367188,
'originating_direction': 'inbound',
'participants': [{'external_contact_id': None,
'external_organization_id': None,
'flagged_reason': None,
'participant_id': 'c844a53c-70bf-441d-81ce-cccd572baf08',
'participant_name': '',
'purpose': 'customer',
'sessions': [{'active_skill_ids': None,
'acw_skipped': None,
'address_from': None,
'address_other': None,
'address_self': None,
'address_to': None,
'agent_assistant_id': None,
'agent_bullseye_ring': None,
'agent_owned': None,
My current code looks like this.
<table class="table table-bordered table-success">
{% for key, value in data.items %}
<tr>
<td> {{ key }} </td>
<td> {{ value }} </td>
</tr>
{% endfor %} {% endblock %}
</table>
I do not want participants like the way it is below and want to continue displaying the key, value of every item in the dictionary as the items above participants.
anon11147534 | 2022-03-29 15:36:08 UTC | #2
Hi Chris,
It seems like you're using django templating. It would be best to consult the django docs on how to handle nested dictionaries.
Chris_Carr | 2022-03-30 08:04:52 UTC | #3
Hi Ronan,
Thank you, would you know how I could do this using python by any chance?
Thank you.
anon11147534 | 2022-03-30 08:57:43 UTC | #4
Hi Chris,
No sorry, I'm not familiar with the Django framework.
Chris_Carr | 2022-03-30 09:20:51 UTC | #5
I meant in regular python and not Django.
Kind Regards, Chris Carr
anon11147534 | 2022-03-30 09:36:14 UTC | #6
Hi Chris,
Real Python is usually my source for solving python problems. This tutorial on iterating through dictionaries should be of some help: https://realpython.com/iterate-through-dictionary-python/
Chris_Carr | 2022-03-31 14:10:14 UTC | #7
Thank you, looks like recursion is the way to go here. I will try it out and update the post.
system | 2022-05-01 14:10:59 UTC | #8
This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.
This post was migrated from the old Developer Forum.
ref: 14042