title: Python sorting key function supporting tuples and lists
body: <p>In Python you can for example sort tuples <code>sorted([(2,2),(1,2),(2,1),(1,1)])</code> and get <code>[(1, 1), (1, 2), (2, 1), (2, 2)]</code>.</p>
  
  <p>You can also use custom key functions <code>sorted([x, y, z], key=custom_key)</code> to implement special sorting criteria.</p>
  
  <p>For example:</p>
  
  <pre><code>medals_map = {'Gold': 1, 'Silver': 2, 'Bronze': 3 }
  def custom_key(item):
      if isinstance(item, basestring):
         try:
             return medals_map[item]
         except KeyError:
             pass
      return item
  </code></pre>
  
  <p>Could sort any list of strings normally, and also gold/silver/bronze according to my custom ordering.</p>
  
  <p>But it does not "correctly" handle tuples anymore: <code>sorted([('Gold', 2), ('Bronze', 1), ('Gold', 1)], key=custom_key)</code> results in <code>[('Bronze', 1), ('Gold', 1), ('Gold', 2)]</code> as item is not a <code>basestring</code>.</p>
  
  <p>But I would like to get <code>[('Gold', 1), ('Gold', 2), ('Bronze', 1)]</code> by somehow applying the custom logic to each item in the tuple.</p>
  
  <p>What's the best way to "map" the custom key logic over any tuples (that may or may not appear) in the list like the default key function does?</p>
  
  <hr>
  
  <p>Edit: More examples:</p>
  
  <ul>
  <li><code>sorted(['Silver', 'Bronze', 'Gold'], key=custom_key)</code>
  
  <ul>
  <li><code>['Gold', 'Silver', 'Bronze']</code></li>
  </ul></li>
  <li><code>sorted([['Silver', 2], ['Silver', 1], ['Gold', 1]], key=custom_key)</code>
  
  <ul>
  <li><code>[['Gold', 1], ['Silver', 1], ['Silver', 2]]</code></li>
  </ul></li>
  <li><code>sorted([(2, 'Gold'), (2, 'Bronze'), (1, 'Gold')], key=custom_key)</code>
  
  <ul>
  <li><code>[(1, 'Gold'), (2, 'Gold'), (2, 'Bronze')]</code></li>
  </ul></li>
  <li><code>sorted([('Silver', 'Bronze'), ('Gold', 'Bronze'), ('Silver', 'Gold')], key=custom_key)</code>
  
  <ul>
  <li><code>[('Gold', 'Bronze'), ('Silver', 'Gold'), ('Silver', 'Bronze')]</code></li>
  </ul></li>
  </ul>