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

python inline append to list and create a variable

I have syntax error in this line:

data = [{'name': project['name'], 'id': project['id']} for projects in api.all_projects() project = projects.to_dict() if project['name'].startswith('name')]

I’m not sure where’s the problem.

Error message when I run the python cli:

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

>>> data = [{'name': project['name'], 'id': project['id']} for projects in api.all_projects() project = projects.to_dict() if project['name'].startswith('name')]
  File "<stdin>", line 1
    data = [{'name': project['name'], 'id': project['id']} for projects in api.all_projects() project = projects.to_dict() if project['name'].startswith('name')]
                                                                                              ^^^^^^^
SyntaxError: invalid syntax

>Solution :

You can’t perform arbitrary assignment after the iteration statement; project = projects.to_dict() is not allowed where you put it.

You can use the walrus operator/assignment expression (:=) from 3.8+ as part of the if to achieve a similar effect though:

data = [{'name': project['name'], 'id': project['id']}
        for projects in api.all_projects()
        if (project := projects.to_dict())['name'].startswith('name')]

Other solutions, which avoid leaking the walrus-assigned variable, but are typically more verbose or ugly in one way or another, include:

  1. Second loop over one-tuple or one-element list:

    data = [{'name': project['name'], 'id': project['id']}
            for projects in api.all_projects()
            for project in [projects.to_dict()]  # for project in (projects.to_dict(),) also works
            if (project['name'].startswith('name')]
    
  2. map-ing with known class’s method (if you actually know the class), or operator.methodcaller:

    data = [{'name': project['name'], 'id': project['id']}
            for project in map(Project.to_dict, api.all_projects())
            if (project['name'].startswith('name')]
    
    # Without knowing class
    from operator import methodcaller  # At top of file
    
    data = [{'name': project['name'], 'id': project['id']}
            for project in map(methodcaller('to_dict'), api.all_projects())
            if (project['name'].startswith('name')]
    
  3. chepner’s inner generator expression solution (a somewhat more verbose Python-level version of the map-based solutions)

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