I want to construct a list containing some dict using list comprehensions. I tried to use .update()
but get None
.
For examples
>>> data = [ {}.update({'demo': i}) for i in range(5) } ]
>>> data
[None, None, None, None, None]
While what I want is:
[ {'demo': 0}, {'demo': 1}, {'demo': 2}, {'demo': 3}, {'demo': 4}]
Any idea? thx.
As for coldspeed's confusion, the example above is just a simple demo, what if {}
is not an empty directory but arbitrary directory? If we just need to update each item which is dict in a list using list comprehensions, I think maybe we can't avoid update
. That's why I came up with .update()
.
Sorry for my poor information in the first time.