2.4

# Using lists

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

players = ['Harry', 'Hermione', 'Neville', 'Ginny']


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. We 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 we have to do this multiple times to split the players into our 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 = ['Harry', 'Hermione', 'Neville', 'Ginny']

# Pick a player, add them to the team list and remove from players.
player_picked = choice(players)
team.append(player_picked)
players.remove(player_picked)



Notice that in order to use the choice function to randomly pick a player from the list, we’ve first had 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, we’d also like to be able to add some players to a different team. We 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 = ['Harry', 'Hermione', 'Neville', 'Ginny']

player_picked = choice(players)
team.append(player_picked)
players.remove(player_picked)

# Add a player to each team


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

## Repeating for all players

Now that we’ve worked out how to add players to teams, we 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 we could use for splitting players between teams. Pick one of the following algorithms to implement, and share your code in the comments.

• Shuffle the list of players and then repeatedly use the .pop list method to remove a player from the end of the list to add to a team.
• You’ll need to import the shuffle function from the random library and use it to shuffle the list:
from random import shuffle
shuffle(list)    #Randomises the order of the whole list

• Iterate through the list of players, and for each player, randomly assign them to a team. You’ll need to make sure that you don’t end up with uneven teams!
• If you represent the two teams in a list, you can use the choice function, which we used above for picking a random player from the list of players.
• Work out the number of players needed in one team, and use the sample function to randomly pick that number of players from the list. Then add the rest of the players to the second team.
• You’ll need to import the sample function from the random library. The following code would set my_sample to be a list containing three random elements from the list my_list:

from random import sample

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


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 = ['Harry', 'Ginny']
players = ['Harry']
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. Share your code, as well as any problems you encounter.