I have a long list of file paths, like:
images = ['path/to/1.png', 'path/to/2.png']
I know that I can wrap this list in a map iterator, which provides sequential access to items in list mapped through a function, like:
image_map = map(cv2.imread, images)
Then I can lazily load those image files as I iterate over the list:
next(image_map)
=> pixels
But I want random access to the original list, mapped through my map function:
image_map[400]
=> pixels
I don't want to convert it to a list
, because that would load all of my images into memory, and they don't fit into memory:
# Bad:
list(image_map)[400]
Another way to think about this might be a decorator on list.__getitem__
.
I know that I can sub-class a list, but I'm really hoping for a cleaner way of doing this.