Using a for loop, the following python code works.
for item in results:
item ['currentservertime'] = int(time.time())
I would like to do it with a list comprehension however. So I have tried the following, but i get a syntax error on the =
item['currentservertime'] = int(time.time()) for item in results
where am i going wrong?
A list comprehension won’t work here, since you’re not at any time building a list – you are changing values in various dictionaries. A list comprehension would be the right tool if your original code was of the form:
Which would translate into the list comprehension:
As it stands, your existing loop is the clearest and most direct way to implement what you’re doing.