Skip to content

List comprehension failing with an empty list #236

Closed
@rccoleman

Description

@rccoleman

The following very simple list comprehension works fine in the interactive Python interpreter, but fails if used in PyScript:

[x for x in []]

I would expect it to return an empty list ([]), as it does in the interactive interpreter:

>>> [x for x in []]
[]

but PyScript gives an error:

        [x for x in []]
                    ^
KeyError: 'x'

It's not a very interesting example, but I've had situations where an incoming iterable may be empty and it's easier to just execute the list comprehension without having to add an if xxx to the end to filter empty lists. I've worked around it with a for loop, but I didn't see a limitation explicitly called out in the docs and wanted to file an issue for it.

Weirdly, I have seen this construct work as expected sometimes, but I can get to to reliably fail if I write an app like this:

@time_trigger('startup')
def startup():
    [x for x in []]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions