To create a list in python, a solution is to use list comprehension, example:
>>> l = [i for i in range(10)]
>>> l
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Now, it is also possible to add a if ... else condition:
>>> l = [-1 if i < 5 else 1 for i in range(10)]
>>> l
[-1, -1, -1, -1, -1, 1, 1, 1, 1, 1]
References
Links | Site |
---|---|
if/else in Python's list comprehension? | stackoverflow |
List Comprehensions in Python | pythonforbeginners |
Multiple If/else in list comprehension python | stackoverflow |
Is it possible to use 'else' in a python list comprehension? | stackoverflow |