-2

I have the following:

list = ["player1", "player2", "player3"]

I want to find all possible combinations of two elements of that list, but with no duplicates. I have tried to work with the itertools.combinations() but without the desired result.

I'm looking for a result like this:

player1, player2
player1, player3
player2, player3

Can someone help me in the right direction?

Thanks in advance

letsdothis
  • 233
  • 1
  • 2
  • 14
  • 1
    `list(itertools.combinations(lst, 2))`? Don't call your list `list` because it overwrites a builtin. – ggorlen Feb 16 '20 at 18:35

1 Answers1

1

You can try this, as it makes use of combinations so import it from itertools like this:

from itertools import combinations

#also it's good practice not to use list, so for sake of it call it something else
listPlayers = ["player1", "player2", "player3"] 

getPairPlayers = sorted(map(sorted, combinations(set(listPlayers), 2)))

print(getPairPlayers)

Output:

[['player1', 'player2'], ['player1', 'player3'], ['player2', 'player3']]
de_classified
  • 1,927
  • 1
  • 15
  • 19