3

I want to group every element in the list with all other elements in the list

for ex -

l1 = [1,2,3]
l2 = [(1,2),(1,3),(2,3)]

I try using zip:

l2 = list(zip(l1,l1[1:]))

but it gives me:

l2 = [(1, 2), (2, 3)]

DESIRED OUTPUT:

[(1,2),(1,3),(2,3)]

for

[1,2,3]

1 Answers1

10

Its what that itertools.combinations is for :

>>> l1 = [1,2,3]
>>> from itertools import combinations
>>> list(combinations(l1,2))
[(1, 2), (1, 3), (2, 3)]
Mazdak
  • 105,000
  • 18
  • 159
  • 188