Apr-19-2020, 02:02 AM
You can generate combinations with itertools.
from itertools import combinations print(list(combinations(range(1,5), 3)))I don't understand what you're trying to do in your question about if/then in the list comprehension. It is possible, you just have to order it properly.
>>> [x if x % 2 else x**2 for x in range(10)] [0, 1, 4, 3, 16, 5, 36, 7, 64, 9]But if you wanted tuples with items from 1 to 5, but where both aren't bigger than 3, there's no else statement. You could do it like:
>>> [(x, y) for x in range(5) for y in range(5) if x < 3 or y < 3] [(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (1, 0), (1, 1), (1, 2), (1, 3), (1, 4), (2, 0), (2, 1), (2, 2), (2, 3), (2, 4), (3, 0), (3, 1), (3, 2), (4, 0), (4, 1), (4, 2)]