So I have these two lists:
image_names = ["IMG_1.jpg", "IMG_2.jpg"]
data = [{"name": "IMG_1.jpg", "id": "53567"},
{"name": "IMG_2.jpg", "id": "53568"},
{"name": "IMG_3.jpg", "id": "53569"},
{"name": "IMG_4.jpg", "id": "53570"}]
I want to search for the first item then the next one and so on in images_names in data and if it has the same name to get the id and add it to a list.
This is how I'm doing this:
for image_name in image_names:
for datum in data:
datum_name = datum.get("name", None)
if datum_name == image_name:
images_ids.append(datum.get("id", None))
Right now it works great but I think this is really inefficient once I get a lot of data in images_names and data. What's the best way in Python to do this? I'm using python 2.7
The main problem is that your data structure isn't set up to give you the access you want. Instead of a list of dicts, make this the natural dict that you want to use:
data = {"IMG_1.jpg": "53567",
"IMG_2.jpg": "53568",
"IMG_3.jpg": "53569",
"IMG_4.jpg": "53570"}
Now, all you need to make the list of corresponding id
s is
images_ids = [data[img] for img in image_names]
If you have a need for both methods of access (if you still need the name
and id
labels), then I recommend that you learn to use a Pandas data frame, with name
and id
as the columns. This will give you the best of both methods.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With