if not f.endswith('.png') for f in filenames: filenames.remove(f) Along with lots of warnings to copy from one list to another rather than delete on an iterating list. But really how much harm could it do? lol... I think.