Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)
closed by

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?

closed

4 Answers

0 votes
by (19k points)
selected by
 
Best answer
Yes, you can write the code in a more concise and Pythonic way using a list comprehension and the any() function:

if any(file_name.endswith(extension) for extension in extensions):

    # do stuff

This approach eliminates the need for a separate for loop and condenses the logic into a single line.
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.           

0 votes
by (25.7k points)

Yes, you can achieve the desired functionality in a more concise and Pythonic way by using a list comprehension and the any() function. Here's an example:

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

file_name = 'test.mp3'

if any(file_name.endswith(extension) for extension in extensions):

    # do stuff

In this code, any() is used to check if any of the extensions in the extensions list match the ending of the file_name. The expression file_name.endswith(extension) for extension in extensions is a generator expression enclosed within any(). It iterates over each extension in the extensions list and checks if file_name ends with that extension.

If any of the conditions in the generator expression evaluate to True, any() returns True, and the code inside the if statement is executed. This approach eliminates the need for an explicit for loop and condenses the logic into a single line, making the code more concise and Pythonic.

0 votes
by (15.4k points)

Yes, there is a more concise and Pythonic way to write the code you provided. You can use a combination of a list comprehension and the any() function to achieve the desired functionality. Here's an example of how you can rewrite the code:

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

file_name = 'test.mp3'

if any(file_name.endswith(extension) for extension in extensions):

    # Perform the necessary actions

In this code, the any() function is used along with a generator expression to iterate over each extension in the extensions list. The generator expression checks if the file_name ends with the current extension. If any of the conditions evaluate to True, indicating a match, any() returns True, and the code inside the if statement is executed.

By using this approach, you eliminate the need for an explicit for loop and condense the logic into a single line, making the code more concise and adhering to Python's guiding principles of readability and simplicity.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
4 answers
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...