I recently had a requirement to make teams from a pool of around 100 people with 5 members per team each.
Usually, it would take a lot of effort & time to make teams. But in the end, you'll be called biased anyways. That's why I want to automate this process. So I wrote a small script in python which I want to share with you now.
- Generate teams from excel sheet
- Form teams of 5 members from 102 people
- display the left out, 2 people
- Completely random logic of forming teams
Let's get started!
I used Google Sheets to collect info, so I need to convert it to CSV.
here's the data after conversion (delete the first line, as they indicate column names)
I saved the file as
Now comes the main part! The Code :)
.CSV files in Python we need
csv package. Which is already pre-installed. This means you don't need to install anything to follow along :)
# To read data from CSV file import csv # To get random sub-data from random import sample
csv package we imported earlier converts each row in the csv file to a list.
with open('data.csv', 'r') as file: reader = csv.reader(file) for i in reader: print(i)
The above code outputs,
As you can see it converts each row in CSV file into a python list. Awesome right?
Now I created 2 empty lists to store data
# stores the roll numbers of all members in one list persons =  # stores lists of teams (lists inside inside list) teams = 
Now I only want the roll numbers of students to be in
persons list. To do so we need to access it by using its index.
with open('data.csv', 'r') as file: reader = csv.reader(file) for i in reader: persons.append(i)
Now comes the main part! forming actual teams.
The idea is to randomly pick numbers from the
persons lists and make those 5 as a list and insert them into the
teams list after inserting them into the
teams list I need to remove those numbers from the
persons list as there might be a chance a single number enters into 2 teams.
To do so we need
random package. here's the code
# Repeat the process until there are less than 5 members left while len(persons)>5: # picks 5 random numbers from persons list a = sample(persons,5) # add the list of numbers as a single team teams.append(a) # after adding remove them to avoid redundancy for i in a: persons.remove(i)
That's it! now we got our required teams in the
import csv from random import sample persons =  teams =  with open('data.csv', 'r') as file: reader = csv.reader(file) for i in reader: persons.append(i) while len(persons)>5: a = sample(persons,5) teams.append(a) for i in a: persons.remove(i) for i in teams: print(i) print(persons)
(for reference only)