Use endswith with multiple extensions

file, list, python

Solution

Use a tuple for it.

>>> ext = [".3g2", ".3gp", ".asf", ".asx", ".avi", ".flv", \
                        ".m2ts", ".mkv", ".mov", ".mp4", ".mpg", ".mpeg", \
                        ".rm", ".swf", ".vob", ".wmv"]

>>> ".wmv".endswith(tuple(ext))
True
>>> ".rand".endswith(tuple(ext))
False

Instead of converting everytime, just convert it to tuple once.

Problem

I'm trying to detect files with a list of extensions. ``` ext = [".3g2", ".3gp", ".asf", ".asx", ".avi", ".flv", \ ".m2ts", ".mkv", ".mov", ".mp4", ".mpg", ".mpeg", \ ".rm", ".swf", ".vob", ".wmv"] if file.endswith(ext): # how to use the list ? command 1 elif file.endswith(""): # it should be a folder command 2 elif file.endswith(".other"): # not a video, not a folder command 3 ```

Original source