0

I just wanted to print all the team pairs which played in world cup 2019, I have made a simple program for this but it contains a little bit of problem.The problem is that, a pair get repeated 1 time (by printing vice-versa) after it has already been print.Like this --

INDIA AUSTRALIA ----after some other prints of pair---- AUSTRALIA INDIA

THERE'S ALSO A LIST INCLUDED IN THIS PROGRAM BUT THIS WEBSITE IS NOT SHOWING THAT

team = ["INDIA", "AUSTRALIA", "ENGLAND", "PAKISTAN", "SOUTH AFRICA", "AFGHANISTAN", "WEST INDIES", "SRI LANKA", "NEW ZEALAND", "BANGLADESH"]
team1 = ["INDIA", "AUSTRALIA", "ENGLAND", "PAKISTAN", "SOUTH AFRICA", "AFGHANISTAN", "WEST INDIES", "SRI LANKA", "NEW ZEALAND", "BANGLADESH"]
print(team)

for x in team:
  for y in team1:
    if x == y:
      continue
    print(x, y)
DjaouadNM
  • 22,013
  • 4
  • 33
  • 55
Piyush
  • 177
  • 1
  • 8
  • There is a built-in for this! Take a look at this answer: https://stackoverflow.com/questions/464864/how-to-get-all-possible-combinations-of-a-list-s-elements – mgrollins Sep 06 '19 at 23:11

2 Answers2

2

With every new iteration of the outer loop, the inner loop starts back from the start of the list, hence repeating the pairs, you can loop by indices instead, and avoid repeating-permuted pairs and pairs where both elements are equal:

team1 = ["INDIA", "AUSTRALIA", "ENGLAND", "PAKISTAN", "SOUTH AFRICA", "AFGHANISTAN", "WEST INDIES", "SRI LANKA", "NEW ZEALAND", "BANGLADESH"]

for i in range(len(team1) - 1):
  for j in range(i + 1, len(team1)):
    print(team1[i], team1[j])

Output:

INDIA AUSTRALIA
INDIA ENGLAND
INDIA PAKISTAN
INDIA SOUTH AFRICA
INDIA AFGHANISTAN
INDIA WEST INDIES
INDIA SRI LANKA
INDIA NEW ZEALAND
INDIA BANGLADESH
AUSTRALIA ENGLAND
AUSTRALIA PAKISTAN
AUSTRALIA SOUTH AFRICA
AUSTRALIA AFGHANISTAN
AUSTRALIA WEST INDIES
AUSTRALIA SRI LANKA
AUSTRALIA NEW ZEALAND
AUSTRALIA BANGLADESH
ENGLAND PAKISTAN
ENGLAND SOUTH AFRICA
ENGLAND AFGHANISTAN
ENGLAND WEST INDIES
ENGLAND SRI LANKA
ENGLAND NEW ZEALAND
ENGLAND BANGLADESH
PAKISTAN SOUTH AFRICA
PAKISTAN AFGHANISTAN
PAKISTAN WEST INDIES
PAKISTAN SRI LANKA
PAKISTAN NEW ZEALAND
PAKISTAN BANGLADESH
SOUTH AFRICA AFGHANISTAN
SOUTH AFRICA WEST INDIES
SOUTH AFRICA SRI LANKA
SOUTH AFRICA NEW ZEALAND
SOUTH AFRICA BANGLADESH
AFGHANISTAN WEST INDIES
AFGHANISTAN SRI LANKA
AFGHANISTAN NEW ZEALAND
AFGHANISTAN BANGLADESH
WEST INDIES SRI LANKA
WEST INDIES NEW ZEALAND
WEST INDIES BANGLADESH
SRI LANKA NEW ZEALAND
SRI LANKA BANGLADESH
NEW ZEALAND BANGLADESH
DjaouadNM
  • 22,013
  • 4
  • 33
  • 55
  • Can't understand where is ---team--- list in this code.There's only ---team1--- in this code .Please update. – Piyush Sep 06 '19 at 23:16
  • @Piyush They are equal, so you can use either or both in any order, it doesn't matter unless they have different values or a different order/count of values. – DjaouadNM Sep 06 '19 at 23:20
0

You can print all combinations of two using itertools.combinations().

import itertools

team1 = ["INDIA", "AUSTRALIA", "ENGLAND", "PAKISTAN", "SOUTH AFRICA", "AFGHANISTAN", "WEST INDIES", "SRI LANKA", "NEW ZEALAND", "BANGLADESH"]

print(list(itertools.combinations(team1, 2)))
Diogo Rocha
  • 9,759
  • 4
  • 48
  • 52