Do you have a set of players that need to be sorted into any team, as long as the two teams are even in numbers? Maybe try putting them in different groups. If you have an array of the players, a simple method is to sort them like this:
# The array that has all of the player objects. Here, we'll first shuffle it up.
# Go through the array, adding the first half of the array to a group.
if array_of_players.size() % 2 == 0:
for n in range(array_of_players.size()):
# The "+1" is added here because iterating over a range starts at 0.
if n + 1 <= array_of_players.size() / 2:
Hope this helps.