Satyam Raha Satyam Raha - 3 months ago 21
Python Question

How to combine a list in Python with itself?

I have a list


now I want


How do I do it in Python 3.5


You want two things

  • itertools.combinations(L, 2) to get all pairs of sublists in your list
  • combine each pair to make a new list

To sum up

import itertools
LL = [a + b for a, b in itertools.combinations(L, 2)]