title: Secret Santa using python
body: <p>I am trying to write a code to pick Secret Santas for my cousins. The code I have so far only works for an even number of people, but I have an odd number or cousins.</p>
  
  <pre><code>import random
  
  def Santa(names, draw):
    matches= []
    while names:
      giver = names.pop() #chooses giver and removes name
      receiver = random.choice(draw) #picks random to receive
  
      if giver != receiver:
        matches.append([giver, receiver]) #adds pair
        draw.remove(receiver) #removes person from receiving list
      else:
        names.append(giver) #if names are the same, adds name back to list
    return matches
  
  family1=['cousin1', 'cousin2', 'cousin3', 'cousin4']
  family2=['cousin5', 'cousin6', 'cousin7','cousin8','cousin9','cousin10']
  family3=['cousin11', 'cousin12', 'cousin13','cousin14']
  family4=['cousin15', 'cousin16', 'cousin17']
  family5=['cousin18', 'cousin19']
  
  names = [ 'cousin1', 'cousin2', 'cousin3', 'cousin4','cousin5']
  
  print(Santa(names,names))
  </code></pre>
  
  <p>The error is IndexError: Cannot choose from an empty sequence
  from the line:</p>
  
  <pre><code>receiver = random.choice(draw)
  </code></pre>
  
  <p>One of the constraints I will add in the future is that you can't have one of your siblings or in-laws. Any advice on how to make this work for an odd list of names or the family constraint would be very helpful.</p>
  
  <p>EDIT: </p>
  
  <p>Here is my final code if anyone was looking to do the same thing. </p>
  
  <pre><code>import random
  from copy import copy
  
  def Santa(names, draw):
    matches= []
    while names:
      giver = names.pop()
      receiver = random.choice(draw)
  
      if giver[0] != receiver[0]:
        if giver[1] != receiver[1]:
          matches.append([giver, receiver])
          draw.remove(receiver)
      else:
        names.append(giver)
    return matches
  
  family1=['cousin1', 'cousin2', 'cousin3', 'cousin4']
  family2=['cousin5', 'cousin6', 'cousin7','cousin8','cousin9','cousin10']
  family3=['cousin11', 'cousin12', 'cousin13','cousin14']
  family4=['cousin15', 'cousin16', 'cousin17']
  family5=['cousin18', 'cousin19']
  
  names = [ ('cousin1', 'parent1'), ('cousin2', 'parent1'), ('cousin3', 'parent1'),('cousin4','parent1'),('cousin5','parent2'),('cousin6','parent2'),('cousin7','parent2'),
           ('cousin8','parent3'),('cousin9','parent3'),('cousin10','parent3'),('cousin11','parent4'),('cousin12','parent4'),('cousin13','parent4'),('cousin14','parent5'),
           ('cousin15','parent5'),('cousin16','parent2'),('cousin17','parent2'),('cousin18','parent3'),('cousin19','parent3'),('cousin20','parent2')]
  
  print(Santa(names,copy(names)))
  </code></pre>