Simple rules based grouping algorithm in Python

Hello all, I have a dataset of 150 people. I’m trying to develop a simple rules based algorithm in Python which will match these people into groups of 3 based on the rules below:

  1. Same gender (male or female)
  2. Similar age (plus or minus 5 years)
  3. Same favourite colour
  4. Same favourite food
  5. Same favourite country

The algorithm should group people into 2 if there is not enough data for a 3rd person. When data is added and the algorithm is executed again the 3rd person should be added to that group.

Any help would be much appreciated!

The way I would do this would be to write a Python function that assigns people to groups.
You can use a ‘case’ statement and ‘if’ or ‘when’ condition to group individuals.