a = [pd.read_pickle("my_file"+str(i)+".pkl" if os.path.exists("my_file"+str(i)+".pkl") else your_alternate_value for i in range(1, 18)]
Or as I prefer to format it to prevent such long lines:
a = [
pd.read_pickle("my_file"+str(i)+".pkl"
if os.path.exists("my_file"+str(i)+".pkl")
else your_alternate_value
for i in range(1, 18)
]
List comprehension are a little hard for me to figure how to format. There might be another way that I would prefer, but it hasn't come to me yet.
Explanation
A list comprehension translates to a format like this
some_list = []
for ...:
for/if ...
...
...
some_list.append(something)
It's however many blocks you want, but only going deeper. Any block can contain only another block or the final appended value. Now, this makes sense when you consider that a list comprehension has no indentation. If you have two if
blocks, how does the comprehension know which one the else
belongs to?
The secret of my solution is that it isn't another block; it's part of what's being appended. The if/else is one expression, comparable to the ternary operator in other languages. Here's what's going on:
some_list = []
for ...
some_list.append(something if blah else some_other_thing)
That's why the if/else is at the beginning.