2 ms·
They can be more readable than that at least, e.g.: keys = "name", "shares", "price" portfolio = [ dict(zip(keys, row)) for row in rows
by nurbl 3y ago
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.