So the thing you're trying to do is just pick random "the length of names
" elements from teams
. In this case, you should use random.sample()
:
>>> import random
>>> person = ['Name', 'Name', 'Name', 'Name', 'Name', 'Name', 'Name', 'Name']
>>> team = ['France', 'Switzerland', 'England', 'Slovakia', 'Germany', 'Ukraine', 'Spain', 'Czech Republic', 'Croatia', 'Italy', 'Republic of Ireland', 'Sweeden', 'Russia', 'Wales', 'Belgium']
>>> random.sample(team, len(person))
['Ukraine', 'Russia', 'England', 'Croatia', 'France', 'Spain', 'Italy', 'Wales']
From the documentation:
random.sample(population, k)
Return a k
length list of unique elements chosen from the population sequence or set. Used for random sampling without replacement.
Returns a new list containing elements from the population while leaving the original population unchanged. The resulting list is in selection order so that all sub-slices will also be valid random samples. This allows raffle winners (the sample) to be partitioned into grand prize and second place winners (the subslices).
If you want to assign two teams per person, I'd suggest you random.shuffle()
the team
list, then split the list into 2 sized chunks, and put the result into a dictionary:
{person: two_teams for person, two_teams in
zip(people, [team[i:i+2] for i in range(0, len(team), 2)])}