I have a source list of images which looks like this (simplified for brevity):
images = [
{
'url': 'https://example.com/image.jpg',
'description': 'Caption',
'type': 'photograph',
'order': 1
},
{
'url': 'https://example.com/image.jpg',
'description': 'Caption',
'type': 'photograph',
'order': 2
}
]
And I'm using a dictionary comprehension inside a list comprehension to remove 2 dictionary items and build a new list of cleaned dictionaries:
images_cleaned = [{k:v for k,v in i.items() if k != 'order' and k != 'type'} for i in images]
I then return the new list at the end of my function:
return images_cleaned
This is inside a list of properties (real estate) which contain a list of images available via a separate request. For 25 properties the code works fine, but then I get to the 26th and it trips up with an error:
UnboundLocalError: local variable 'images_cleaned' referenced before assignment
Looking at the images available for this property however, doesn't reveal anything different. It contains the same list of images.
Is there anything noticeably wrong with my images_cleaned
list and dictionary comprehension which would result in nothing behind assigned to images_cleaned
variable before returning? It's my assumption that even if there were no images then the variable would still be an empty list []
?
Edit: Specifically the error occurs on the return
statement, returning images_cleaned
.