Say I have a list of filenames files containing data in json format. To receive the data in a list with an entry for each file, I use a list comprehension:
>>> import json
>>> data = [json.load(open(file)) for file in files]
Now I was wondering, if there is a way to append the file name file to the json data, as if it looked like this:
{
'Some': ['data', 'that', 'has', 'already', 'been', 'there'],
'Filename': 'filename'
}
For my case, json.load() returns a dict, so I've tried something similar to this question. This didn't work out for me, because files contains strings and not dictionaries.
Edit
For clarification, if dict.update() didn't return None, this would probably work:
>>> data = [dict([('filename',file)]).update(json.load(open(file))) for file in files]