Using lists

5 comments

In this step, you’re going to create a list of players and use it to split those players into two random teams. I am going to demonstrate one way of solving this problem, but then I would like you to try to find an alternative method, using a different way of manipulating lists.

You’ll start with a list of players:

players = ['Kane', 'Pogba', 'Marta', 'Kerr']

Feel free to create your own list. Be careful to make sure that each element of the list is a string (i.e. enclosed within a matching pair of single or double quotation marks), and that the elements are separated by commas.

Picking players

Picking a player is not as simple as just choosing one element from the list at random, even though that’s the first step. You also need to make sure that the player is added to one of the teams, as well as removing that player from the original list so that they can’t be picked again. As you will have to do this multiple times to split the players into your two teams, this is a good candidate for turning into a function.

from random import choice # adds the choice function from the random library


# Create an empty team list and players list for the pool of players

team = []

players = ['Kane', 'Pogba', 'Marta', 'Kerr']


# define the add_player_to_team function

def add_player_to_team():

    player_picked = choice(players)     # Pick a player

    team.append(player_picked)          # Add them to the team list

    players.remove(player_picked)       # Remove from original players list

add_player_to_team() # Call the add_player_to_team function once

Notice that in order to use the choice function to randomly pick a player from the list, you’ll first have to import it from the random library.

Adding players to different teams

At the moment this program will pick one player from the players list, add them to an initially empty list called team, and remove them from the players list. However, you’ll also need to add some players to a different team. You can manage this by passing the desired team to the function as a parameter:

from random import choice


teamA = []

teamB = []    # Create two empty lists for teams A and B


players = ['Kane', 'Pogba', 'Marta', 'Kerr']


def add_player_to_team(team):

player_picked = choice(players)

team.append(player_picked)

players.remove(player_picked)

# Add a player to each team

add_player_to_team(teamA)

add_player_to_team(teamB)

This code will now add one player to team A, and one to team B.

Repeating for all players

Now that you’ve worked out how to add players to teams, you need to keep going until all of the players have been allocated to one of the teams. Using a loop, and making sure to not try to pick a player from an empty list, finish off this program to split the players into two teams, teamA and teamB. Make sure to print the two teams at the end of the program to make sure it’s worked, and share your code in the comments.

Alternative algorithms

Rather than picking a random player each time to add to the teams in turn, there are a few other algorithms you could use for splitting players between teams. Pick one of the following algorithms to implement, and share your code in the comments.

   from random import shuffle

    shuffle(list)    #Randomises the order of the whole list

from random import sample

my_sample = sample(my_list,3) # Randomly select 3 items from my_list and creates my_sample

Testing your algorithm

An important part of coding is testing your program by making sure that it works in all situations. Try the following lists of players, and try each of them multiple times to make sure that your code is correctly randomising the teams.

players = ['Kane', 'Marta'

players = ['Kane']

players = []

players = ['A', 'B', 'C', 'D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R']

What problems might these examples show up? How did you fix them? Share your experience in the comments.

Extending the functionality

Sometimes it may be necessary to split a list of players into more than two teams. Create a new function which takes as parameters a list of team names and a list of players (both of these will be lists of strings), and allocates players to teams as evenly as possible.

Some of the algorithms discussed earlier may make it easier or harder for you to do this.