Project 4: Photo Gallery
Python Challenge
Builds on these lessons
Step 1 of 3
Project
Filtering Filenames with a List Comprehension
Filter a list of filenames down to just images with [p for p in photos if p.endswith((".jpg", ".png"))]. A list comprehension packs a filter-and-transform loop into one readable expression, instead of an empty list plus a for loop plus .append().
🎯 Your Task
Please add the exact code shown in the light gray box below to your editor.Do not delete your previous code, just insert these new lines in the correct place!
photos = ["IMG_1.jpg", "IMG_2.png", "notes.txt", "IMG_3.jpg"]
image_files = [p for p in photos if p.endswith((".jpg", ".png"))]
print(image_files)