user2399453
user2399453

Reputation: 3081

How to rewrite this using list comprehension

Is there a simple way to rewrite this snippet using list comprehension?

f_list = []
for f in file_list:
    if os.path.isfile(SC_JSON_DIR + f + ".json"):
        f_list.append(f)
return f_list

Upvotes: 0

Views: 233

Answers (1)

zondo
zondo

Reputation: 20336

Try this:

return [f for f in file_list if os.path.isfile(SC_JSON_DIR + f + ".json")]

Upvotes: 8

Related Questions