Andrew Johnson
Andrew Johnson

Reputation: 13286

What is the Pythonic way to write this loop?

for jr in json_reports:
  jr['time_created'] = str(jr['time_created'])

Upvotes: 1

Views: 277

Answers (2)

Fragsworth
Fragsworth

Reputation: 35497

That would be the pythonic way to write the loop if you need to assign it to the same list.

If you just want to pull out strings of all time_created indices in each element of json_reports, you can use a list comprehension:

strings = [str(i['time_created']) for i in json_reports]

Upvotes: 5

Steef
Steef

Reputation: 34665

Looks to me that you're already there

Upvotes: 10

Related Questions