0
we = [1,2,3,4]
for i in we:
    for e in we:
        print('('+str(i)+' - '+str(e)+')')
    #del we[0]

This is the result:

(1 - 1)
(1 - 2)
(1 - 3)
(1 - 4)
(2 - 1)
(2 - 2)
(2 - 3)
(2 - 4)
(3 - 1)
(3 - 2)
(3 - 3)
(3 - 4)
(4 - 1)
(4 - 2)
(4 - 3)
(4 - 4)

But I do not want the same elements to repeat like I have (1 - 3) so I do not want (3 - 1) to show and so on. I also need to use for loops in this

CypherX
  • 7,019
  • 3
  • 25
  • 37
Data_sniffer
  • 588
  • 1
  • 8
  • 19

1 Answers1

0

You can do this in a single line using list comprehension and itertools.combinations.

from itertools import combinations
['({} - {})'.format(e[0], e[1]) for e in list(combinations([1,2,3, 4], 2))]

Output:

['(1 - 2)', '(1 - 3)', '(1 - 4)', '(2 - 3)', '(2 - 4)', '(3 - 4)']

References:

  1. Python - how to find all combinations of two numbers in a list
  2. How to get combination of element from a python list?
CypherX
  • 7,019
  • 3
  • 25
  • 37