Satyam Raha Satyam Raha - 1 year ago 92
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

Answer Source

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)]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download