0 votes
1 view
in Python by (12.7k points)

Is there any pythonic way to write the following code:

extensions = ['.mp3','.avi']

file_name = 'test.mp3'

for extension in extensions:

    if file_name.endswith(extension):

        #do stuff

I have an ambiguous memory that the express declaration of the for loop can be kept away from and be written in the if condition. Is this valid?

1 Answer

0 votes
by (26.4k points)

In spite of the fact that not broadly known, str.endswith additionally acknowledges a tuple. You don't have to loop.

>>> 'test.mp3'.endswith(('.mp3', '.avi'))

True

Wanna become a Python expert? Come and join the python certification course and get certified.           

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers !

Categories

...