title: how to sort multiple list of tuple in any direction in python?
body: <p>so basically I have 10 lists: 5 rate lists and 5 dev lists. Which are combined to form 5 lists. Lists are:</p>
  <pre><code>rate1 = [16,16,16,0,0,0,0,0,16,16,0,0,0,0,0,0,12,23,34,45,56,67,545,23,34,45,56]
  rate2 = [6,6,15,5,5,5,5,5,8,7,7,7,7,7,6,6,2,23,324,415,56,67,545,23,34,48,6]
  rate3 = [5,4,3,2,2,0,0,0,8,8,8,8,6,6,6,5,122,23,34,41,5216,67,545,23,34,25,533]
  rate4 = [6,5,2,0,0,0,0,0,9,9,9,7,6,6,5,5,1232,23,34,45,56,67,545,23,34,44,38]
  rate5 = [0,0,0,0,0,0,0,0,10,9,8,7,6,6,6,2,124,243,324,457,562,7,545,33,64,45,56]
  
  dev1 = [7,2,10,-8,-1,-5,-4,-26,12,7,-14,-21,-16,-15,-19,-20,124,24,34,457,562,7,545,33,64,45,56]
  dev2 = [26,19,22,18,17,8,14,26,15,21,29,49,53,30,48,37, 1124,243,24,77,562,7,545,33,64,45,56]
  dev3 = [53,54,40,36,57,-8,-1,-5,49,53,48,37,44,90,45,88, 154,213,34,87,572,7,545,33,64,45,56]
  dev4 = [53,54,57,-8,-1,-5,-4,-26,49,53,48,90,88,55,61,53, 4,241,374,457,52,7,545,33,64,45,56]
  dev5 = [-8,-1,-5,-4,-26,-25,-16,0,90,88,61,79,63,88,82,92, 14,26,324,457,562,7,545,33,64,45,56]
  </code></pre>
  <p>So I simply wrote a program to combine rate1 with dev1 and rest of them. After I divide them in 5 groups like this:</p>
  <pre><code>price1 = list(zip(rate1, dev1))
  price2 = list(zip(rate2, dev2))
  price3 = list(zip(rate3, dev3))
  price4 = list(zip(rate4, dev4))
  price5 = list(zip(rate5, dev5))
  
  n=5
  a = [price1[i:i + n] for i in range(0, len(price1), n)]
  b = [price2[i:i + n] for i in range(0, len(price2), n)]
  c = [price3[i:i + n] for i in range(0, len(price3), n)]
  d = [price4[i:i + n] for i in range(0, len(price4), n)]
  e = [price5[i:i + n] for i in range(0, len(price5), n)]
  print(a)
  print(b)
  print(c)
  print(d)
  print(e)
  </code></pre>
  <p>After doing this I am getting a 5 2d lists and then after that I want to sort them in descending order rate wise and get the top 4 rates of every rate list and from those 4 values of every rate list, get the 4 maximum value from each set and get their corresponding dev value, so to do I wrote this program:</p>
  <pre><code>####Sorting the tuples of list rate wise####
  newPrice1 = [sorted(block, reverse=True, key=lambda x: x[0])[:4] for block in a]
  newPrice2 = [sorted(block, reverse=True, key=lambda x: x[0])[:4] for block in b]
  newPrice3 = [sorted(block, reverse=True, key=lambda x: x[0])[:4] for block in c]
  newPrice4 = [sorted(block, reverse=True, key=lambda x: x[0])[:4] for block in d]
  newPrice5 = [sorted(block, reverse=True, key=lambda x: x[0])[:4] for block in e]
  
  ###Getting the max all the list and their coresponding Dev###
  rv = []
  dv = []
  for i in range(0, len(newPrice1)-1):
      rv.append([])
      dv.append([])
      for j in range(4):
          m = max(newPrice1[i][j][0], newPrice2[i][j][0], newPrice3[i][j][0], newPrice4[i][j][0], newPrice5[i][j][0])
          rv[-1].append(m)
          if m == newPrice1[i][j][0]:
              dv[-1].append(newPrice1[i][j][1])
  
          elif m == newPrice2[i][j][0]:
              dv[-1].append(newPrice3[i][j][1])
  
          elif m == newPrice3[i][j][0]:
              dv[-1].append(newPrice3[i][j][1])
  
          elif m == newPrice4[i][j][0]:
              dv[-1].append(newPrice4[i][j][1])
  
          elif m == newPrice5[i][j][0]:
              dv[-1].append(newPrice5[i][j][1])
  
  print(newPrice1)
  print(newPrice2)
  print(newPrice3)
  print(newPrice4)
  print(newPrice5)
  print(rv)
  print(dv)
  </code></pre>
  <p>Output:</p>
  <pre><code>newPrice1 = [[(16, 7), (16, 2), (16, 10), (0, -8)], [(16, 12), (16, 7), (0, -5), (0, -4)], [(0, -14), (0, -21), (0, -16), (0, -15)], [(45, 457), (34, 34), (23, 24), (12, 124)], [(545, 545), (67, 7), (56, 562), (34, 64)], [(56, 56), (45, 45)]]
  newPrice2 = [[(15, 22), (6, 26), (6, 19), (5, 18)], [(8, 15), (7, 21), (5, 8), (5, 14)], [(7, 29), (7, 49), (7, 53), (7, 30)], [(415, 77), (324, 24), (23, 243), (6, 37)], [(545, 545), (67, 7), (56, 562), (34, 64)], [(48, 45), (6, 56)]]
  newPrice3 = [[(5, 53), (4, 54), (3, 40), (2, 36)], [(8, 49), (8, 53), (0, -8), (0, -1)], [(8, 48), (8, 37), (6, 44), (6, 90)], [(122, 154), (41, 87), (34, 34), (23, 213)], [(5216, 572), (545, 545), (67, 7), (34, 64)], [(533, 56), (25, 45)]]
  newPrice4 = [[(6, 53), (5, 54), (2, 57), (0, -8)], [(9, 49), (9, 53), (0, -5), (0, -4)], [(9, 48), (7, 90), (6, 88), (6, 55)], [(1232, 4), (45, 457), (34, 374), (23, 241)], [(545, 545), (67, 7), (56, 52), (34, 64)], [(44, 45), (38, 56)]]
  newPrice5 = [[(0, -8), (0, -1), (0, -5), (0, -4)], [(10, 90), (9, 88), (0, -25), (0, -16)], [(8, 61), (7, 79), (6, 63), (6, 88)], [(457, 457), (324, 324), (243, 26), (124, 14)], [(562, 562), (545, 545), (64, 64), (33, 33)], [(56, 56), (45, 45)]]
  
  
  Rate = [[16, 16, 16, 5], [16, 16, 5, 5], [9, 8, 7, 7], [1232, 324, 243, 124], [5216, 545, 67, 34]]
  Dev = [[7, 2, 10, 36], [12, 7, -8, -1], [48, 37, 44, 90], [4, 87, 26, 14], [572, 545, 7, 64]]
  </code></pre>
  <p>So Rate list is not right actually because if you see the first set of newPrices you can see the biggest number is <strong>16</strong> but after 3 consecutive <strong>16</strong>, there is <strong>5</strong> in the **4th(3rd index)**place But in actual there should be <strong>15</strong> over there which is present in the 0th index in <code>newPrice2</code>. And that is happening maybe because I am not taking the whole matrix at a time. The expected output is:</p>
  <pre><code>Rate = [[16, 16, 16, 15], [16, 16, 10, 9], [9, 8, 8, 8], [1232, 457, 415, 324], [5216, 562, 545, 545]]
  dev = [[7, 2, 10, 22], [12, 7, 90, 49], [48, 48, 61, 37], [4, 457, 77, 24], [572, 562, 545, 545]]
  </code></pre>
  <p>Can someone please help in the last part where we are getting the max value from any direction?</p>