News for hackers. Stuff that matters.
Re:Advanced Python Mastery – A Course by David Beazley by nurbl on Wednesday July 19, 2023 @11:23AM (#36787951)
They can be more readable than that at least, e.g.:

    keys = "name", "shares", "price"
    portfolio = [
        dict(zip(keys, row))
        for row in rows
    ]
If I had to do more complex stuff than building a dict like this I'd move it into a function. That tends to make the purpose more clear anyway.

That said, it's fine to append to a list too, I just prefer comprehensions when they fit the job. In particular, if you're just going to iterate once over this list anyway, you can turn it into a iterator comprehension by replacing [] by () and save some memory.

There are no comments on this comment yet.