There is a list.

spis = ['qwert', '123', '321'] 

I need to glue each element of the list with each element. Ie in this case it should turn out like this:

 qwert;123 qwert;321 123;qwert 123;321 321;qwert 321;123 

    1 answer 1

    Use itertools.permutations :

     from itertools import permutations result = ' '.join([';'.join(t) for t in permutations(spis, 2)]) print(result) 

    Conclusion:

     qwert;123 qwert;321 123;qwert 123;321 321;qwert 321;123 

    If the result should be a list:

     result = [';'.join(t) for t in permutations(spis, 2)] print(result) 

    Conclusion:

     ['qwert;123', 'qwert;321', '123;qwert', '123;321', '321;qwert', '321;123'] 
    • one
      Another option: print(*map(';'.join, permutations(spis, 2))) or print(*[x+';'+y for x in spis for y in spis if x is not y])
    • @jfs, yes, you have succeeded gracefully! - MaxU