I want to create a dictionary with Key value pairs which are filled via an for Loop
The dictionary I want to achive
[
{
"timestamp": 123123123,
"image": "image/test1.png"
},
{
"timestamp": 0384030434,
"image": "image/test2.png"
}
]
My code does not work and I´m new to the datatypes of python.
images_dict = []
for i in images:
time = Image.open(i)._getexif()[36867]
images_dict = {"timestamp": time, "image": i}
What am I missing?
CodePudding user response:
First, you seem to be confusing the definition of a list and a dictionary in python. Dictionaries use curly brackets {}
and lists use regular brackets []
. So in your first example, you are describing a list with a single element, which is a dictionary.
As for your code example, you are creating an empty list, and then iterating over images
which I assume is a list of images, and then redefining the variable images_dict
to be a dictionary with two key: value
pairs for every iteration.
It seems like what you want is this:
images_dict = {}
for image in images:
time = Image.open(1)._getexif()[36867]
images_dict['timestamp'] = time
images_dict['image'] = image
CodePudding user response:
The answer from Tom McLean worked for me, I´m a little bit confused with the dataypes of python
images_dict.append({"timestamp": time, "image": i})