Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Having trouble understanding List Comprehension in Python

I am trying to wrap my mind around list comprehension based on another question I saw here today. I have customized and simplified the code from the previous question so it isn’t blatantly the same. I am trying to step through converting this code to a traditional set of nested for loops to help me understand it:

employee_records = [('group1', {'values': []}),
                    ('group2',
                        {'values': [
                            {'employee': {'name': 'Joseph', 'id': 103, 'displayName': 'Joe'}, 'category': 'Title1'},
                            {'employee': {'name': 'Robert', 'id': 114, 'displayName': 'Bob'}, 'category': 'Title2'}
                        ]}
                    ),
                    ('group3',
                        {'values': [
                            {'employee': {'name': 'Rachel', 'id': 1563, 'displayName': 'Rach'}, 'category': 'title3'}
                        ]}
                    )]

employee_details = [[nested_list[0], x['employee']['name'], x['category']]
                    for nested_list in employee_records
                    for x in nested_list[1]["values"]]
                   
print(employee_details)

The output is as follows:

[['group2', 'Joseph', 'Title1'], ['group2', 'Robert', 'Title2'], ['group3', 'Rachel', 'title3']]

When I translate this (based on my understanding) into a set of nested for loops, I get a different result:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

employee_records = [('group1', {'values': []}), ('group2', {'values': [\
               {'employee': {'name': 'Joseph', 'id': 103, 'displayName': 'Joe'}, 'category': 'Title1'}, \
               {'employee': {'name': 'Robert', 'id': 114, 'displayName': 'Bob'}, 'category': 'Title2'}]}), \
               ('group3', {'values': [{'employee': {'name': 'Rachel', 'id': 1563, 'displayName': 'Rach'}, 'category': 'title3'}]})]

department = []

for nested_list in employee_records:
    for x in nested_list[1]['values']:
        department.append([[nested_list][0], x['employee']['name'], x['category']])

print(department)

Output:

[[('group2', {'values': [{'employee': {'name': 'Joseph', 'id': 103, 'displayName': 'Joe'}, 'category': 'Title1'}, {'employee': {'name': 'Robert', 'id': 114, 'displayName': 'Bob'}, 'category': 'Title2'}]}), 'Joseph', 'Title1'], [('group2', {'values': [{'employee': {'name': 'Joseph', 'id': 103, 'displayName': 'Joe'}, 'category': 'Title1'}, {'employee': {'name': 'Robert', 'id': 114, 'displayName': 'Bob'}, 'category': 'Title2'}]}), 'Robert', 'Title2'], [('group3', {'values': [{'employee': {'name': 'Rachel', 'id': 1563, 'displayName': 'Rach'}, 'category': 'title3'}]}), 'Rachel', 'title3']]

While I was troubleshooting this, I was able to get the expected result by changing the append statement to be more specific for the first element:

employee_records = [('group1', {'values': []}), ('group2', {'values': [\
               {'employee': {'name': 'Joseph', 'id': 103, 'displayName': 'Joe'}, 'category': 'Title1'}, \
               {'employee': {'name': 'Robert', 'id': 114, 'displayName': 'Bob'}, 'category': 'Title2'}]}), \
               ('group3', {'values': [{'employee': {'name': 'Rachel', 'id': 1563, 'displayName': 'Rach'}, 'category': 'title3'}]})]

department = []

for nested_list in employee_records:
    for x in nested_list[1]['values']:
        department.append([[nested_list][0][0], x['employee']['name'], x['category']])

print(department)

Output:

[['group2', 'Joseph', 'Title1'], ['group2', 'Robert', 'Title2'], ['group3', 'Rachel', 'title3']]

Why do I need to change nested_list[0] to nested_list[0][0] when refactoring this into nested for loops?

>Solution :

You ask

Why do I need to change nested_list[0] to nested_list[0][0]

but your program does not do nested_list[0], it does [nested_list][0], which is just a complicated way of saying nested_list.

More specifically,

Your comprehension is doing:

employee_details = [
    [
        nested_list[0],
        x['employee']['name'],
        x['category']
    ]
    for nested_list in employee_records
    for x in nested_list[1]["values"]
]

While you for loops are doing:

employee_details = []
for nested_list in employee_records:
    for x in nested_list[1]['values']:
        employee_details.append([
            [nested_list][0],    ## <--- extra []
            x['employee']['name'],
            x['category']
        ])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading