1

I have a list of images that I am repeatedly calling the same functions for:

first_image = load_image("test1.jpg")
first_image_encoding = pic_encoding(first_image)[0]

second_image = load_image("test2.jpg")
second_image_encoding = pic_encoding(second_image)[0]

After which I need to populate them into an array like this:

encoding_arr = [
    first_image_encoding, 
    second_image_encoding
]

I am trying to do all of this dynamically but need help with the first part for assigning unique variable names to assigned values.

Here is what I have so far:

for root, dirs, files in os.walk(img_dir):
    for f in files:
        first_image = load_image(f)
        first_image_encoding = pic_encoding(first_image)[0]

I am not sure how to get a list of unique variables here instead of manually hard coding them

HeelMega
  • 458
  • 8
  • 23

1 Answers1

1

Creating variables dynamically adds excessive overhead of handling them, instead you could directly append to list like so:

encoding_arr = []
for root, dirs, files in os.walk(img_dir):
    for f in files:
        encoding_arr.append(pic_encoding(load_image(f))[0])
Austin
  • 25,759
  • 4
  • 25
  • 48