Home > OS >  How to get first element of tuples and values of nested dictionary within list
How to get first element of tuples and values of nested dictionary within list

Time:03-14

How do I select first element of a tuple and values of nested dictionary that's inside a list?

organization = [('research', {'size': 0, 'values': [], 'start': 0}), ('marketing', {'size': 2, 'values': [{'user': {'name': 'anna', 'id': 10, 'displayName': 'Anna'}, 'category': 'Secretary'}, {'user': {'name': 'bob', 'id': 11, 'displayName': 'Bobby'}, 'category': 'Manager'}], 'start': 0}), ('sales', {'size': 1, 'values': [{'user': {'name': 'claire', 'id': 13, 'displayName': 'Clarissa Claire'}, 'category': 'Secretary'}], 'start': 0})]

department = [[x['user']['name'], x['category']] for nest_list in organization for x in nest_list["values"]]
print(department)

Expected:

department = [[marketing, anna, Secretary], [marketing, bob, manager], [sales, claire, Secretary]]

CodePudding user response:

Here is what I came up with:

# Define your list
organization = [('research', {'size': 0, 'values': [], 'start': 0}), ('marketing', {'size': 2, 'values': [{'user': {'name': 'anna', 'id': 10, 'displayName': 'Anna'}, 'category': 'Secretary'}, {'user': {'name': 'bob', 'id': 11, 'displayName': 'Bobby'}, 'category': 'Manager'}], 'start': 0}), ('sales', {'size': 1, 'values': [{'user': {'name': 'claire', 'id': 13, 'displayName': 'Clarissa Claire'}, 'category': 'Secretary'}], 'start': 0})]

# Create empty lists to be used by the loop
employee = []
employee_list = []

# Loop through each department
for counter in range(0,len(organization)):
    # Check if there are any entries by looking at the size key
    if organization[counter][1]['size'] > 0:
        # loop through each user
        for user in range(0,len(organization[counter][1]['values'])):
            # for each user, pull out the Name and Category, and add it to the employee list
            employee = [organization[counter][0],organization[counter][1]['values'][user]['user']['name'],organization[counter][1]['values'][user]['category']]
            # Add each employee as a nested list in the Employee list
            employee_list.append(employee)

print(employee_list)

There may be some refinements that could be made here but this is what I was able to cobble together.

Edit: After I said this I was able to refine it a bit:

# Define your list
organization = [('research', {'size': 0, 'values': [], 'start': 0}), ('marketing', {'size': 2, 'values': [{'user': {'name': 'anna', 'id': 10, 'displayName': 'Anna'}, 'category': 'Secretary'}, {'user': {'name': 'bob', 'id': 11, 'displayName': 'Bobby'}, 'category': 'Manager'}], 'start': 0}), ('sales', {'size': 1, 'values': [{'user': {'name': 'claire', 'id': 13, 'displayName': 'Clarissa Claire'}, 'category': 'Secretary'}], 'start': 0})]

# Create empty lists to be used by the loop
employee = []
employee_list = []

# Loop through each department
for counter in range(0,len(organization)):
    # Loop through each user based on the size key
    for user in range(0,organization[counter][1]['size']):
        # for each user, pull out the Name and Category, and add it to the employee list
        employee = [organization[counter][0],organization[counter][1]['values'][user]['user']['name'],organization[counter][1]['values'][user]['category']]
        # Add each employee as a nested list in the Employee list
        employee_list.append(employee)

print(employee_list)

CodePudding user response:

The issue is in this part:

for nest_list in organization for x in nest_list["values"]

nest_list is a tuple (all the tuples in organization), and you want x to go through all the objects in the values list which is the second member of each tuple, but you jump right to values. So the end there should be nest_list[1]["values"]-- you need to get into the second tuple element BEFORE going into values.

Then in your list defintion you'll also need to include nest_list[0] as the first element of each element list so that you can have the department as you want. The department = line would thus look like this:

department = [[nest_list[0], x['user']['name'], x['category']] for nest_list in organization for x in nest_list[1]["values"]]
  • Related