Search code examples
pythonpython-itertools

Itertools and customizing list


So I have a list of teams:

teams = ["Team A", "Team B", "Team C", "Team D",] # ... etc.

What I'm trying to do is find all the possible match ups that that exist between these teams. I figured out I could do this:

for x in itertools.product(teams,teams):
   teams_list.append([x[0],x[1]])

Eventually what I want to do is run a check to make sure each matchup is in there once, as well as taking each matchup and creating a unique url string from the 2 names. Is this the proper way to go about it, because when i try playing around with the teams_list I have tuple issues.


Solution

  • You want combinations, not product:

    teams_list = list(itertools.combinations(teams, 2))