Home > Software engineering >  How to display data from a dictionary within a list in a readable format?
How to display data from a dictionary within a list in a readable format?

Time:11-20

The data needs to be stored in this format

data = {'admin': [{'title': 'Register Users with taskManager.py', 'description': 'Use taskManager.py to add the usernames and passwords for all team members that will be using this program.', 'due date': '10 Oct 2019', 'date assigned': '20 Oct 2019', 'status': 'No'}, {'title': 'Assign initial tasks', 'description': 'Use taskManager.py to assign each team member with appropriate tasks', 'due date': '10 Oct 2019', 'date assigned': '25 Oct 2019', 'status': 'No'}], 'new user': [{'title': 'Take out trash', 'description': 'Take the trash can down the street', 'due date': '10 oct 2022', 'date assigned': '20 Oct 2022', 'status': 'No'}]}

I need to display this data like this:

user: admin

title: Register Users with taskManager.py
description: Use taskManager.py to add the usernames and passwords for all team members that will be using this program
date assigned: 10 Oct 2019
due date: 20 Oct 2022
status: No


title: Assign initial tasks
description: Use taskManager.py to assign each team member with appropriate tasks
date assigned: 10 Oct 2019
due date: 25 Oct 2019
status: No

user: new user

title: Take out trash
description: Take the trash can down the street
date assigned: 10 Oct 2022
due date: 20 Oct 202
status: No

How do I do this?

CodePudding user response:

Try this:

dict = #your dict here
for user in dict.values():
  print(f"user: {user}")
  for k, v in dict[user]: # selects sub dicts
    print (f"{k}: {v})

CodePudding user response:

You basically have a densely nested structure so if this is the final structure of your data then the easiest way is unravel it in a hard coded way using dict.items() like so:

data = {'admin': [{'title': 'Register Users with taskManager.py', 'description': 'Use taskManager.py to add the usernames and passwords for all team members that will be using this program.', 'due date': '10 Oct 2019', 'date assigned': '20 Oct 2019', 'status': 'No'}, {'title': 'Assign initial tasks', 'description': 'Use taskManager.py to assign each team member with appropriate tasks', 'due date': '10 Oct 2019', 'date assigned': '25 Oct 2019', 'status': 'No'}], 'new user': [{'title': 'Take out trash', 'description': 'Take the trash can down the street', 'due date': '10 oct 2022', 'date assigned': '20 Oct 2022', 'status': 'No'}]}


for user, tasks in data.items():
    print("user:", user)
    for task in tasks:
        print()
        for field, value in task.items():
            print(f"{field}: {value}")
        print()

This produces the desired output:

user: admin

title: Register Users with taskManager.py
description: Use taskManager.py to add the usernames and passwords for all team members that will be using this program.
due date: 10 Oct 2019
date assigned: 20 Oct 2019
status: No


title: Assign initial tasks
description: Use taskManager.py to assign each team member with appropriate tasks
due date: 10 Oct 2019
date assigned: 25 Oct 2019
status: No

user: new user

title: Take out trash
description: Take the trash can down the street
due date: 10 oct 2022
date assigned: 20 Oct 2022
status: No

CodePudding user response:

Loop over the keys to print the data. You may want to add extra formatting for readability afterward.

for i in data.keys():
    print(f"user: {i}")
    for j in data[i]:
        for k in j.keys():
            print(f"{k}: {j[k]}")
  • Related