I know I asked this before, but I'm still not sure why I just get an empty list when I test this
def sorted_images(image_dict):
'''(dict) -> list of strGiven an image dictionary return a list of the filenames
sorted by date. >>> d = {'image1.jpg': ['UTSC', '2017-11-03','Happy Friday'], \
'image2.jpg': ['UTSC', '2017-11-04', 'Happy Sat.']}
>>> sorted_images(d)
['image1.jpg', 'image2.jpg']
'''
new_list = []
for filename, (location, date, caption) in image_dict.items():if filename not in image_dict:new_list.append(filename)
return new_list
Your list is empty because this condition is always false
if filename not in image_dict:
filename
is a key in image_dict
.
To get the output specified in the docstring, you can do something like this:
def sorted_images(image_dict):'''(dict) -> list of strGiven an image dictionary return a list of the filenamessorted by date. >>> d = {'image1.jpg': ['UTSC', '2017-11-03','Happy Friday'], \'image2.jpg': ['UTSC', '2017-11-04', 'Happy Sat.']}>>> sorted_images(d) ['image1.jpg', 'image2.jpg']'''return [k for k, v in sorted(image_dict.items(), key=lambda x: x[1][1])]