'Python - Write all tuples of a combination using list comprehension
I would like to create all pairs (i, j) such that i goes from 0 to n-1 and j goes from i to n-1. Basically these are all the unique combinations for two lists of length n.
As an example if n=3 then I would like to get
[(0, 0), (0, 1), (0, 2), (1, 1), (1, 2), (2, 2)]
It would be great if I could do this with a list comprehension. The long way around would be
n = 3
tuples = []
for i in range(n):
for j in range(i, n):
tuples.append((i, j))
I have tried this list comprehension, unsuccessfully
tuples = [(i,j) for i in range(3) and j in range(i, 3)]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
