title: Sorting tuples by element value in Python
body: <p>I need to sort a list of tuples in Python by a specific tuple element, let's say it's the second element in this case. I tried</p>
  
  <pre><code>sorted(tupList, key = lambda tup: tup[1])
  </code></pre>
  
  <p>I have also tried</p>
  
  <pre><code>sorted(tupList, key = itemgetter(1))
  '''i imported itemgetter, attrgetter, methodcaller from operator'''
  </code></pre>
  
  <p>but the list was returned the same both times. I checked </p>
  
  <p><a href="https://stackoverflow.com/questions/4554115/sorting-tuples-in-python-with-a-custom-key">sorting tuples in python with a custom key</a></p>
  
  <p><a href="https://stackoverflow.com/questions/4421410/sort-tuples-in-lists-in-python">sort tuples in lists in python</a></p>
  
  <p><a href="https://wiki.python.org/moin/HowTo/Sorting" rel="nofollow noreferrer">https://wiki.python.org/moin/HowTo/Sorting</a></p>