-7

I have a list something like below- Xlist -

[('00:04:F2:FF:7A:1D'    -88), ('4C:49:E3:64:9D:D6'  -63),  
 ('00:EC:0A:87:61:F8'    -58), ('CC:9F:7A:99:0D:EA'  -80)]

I need the list to be sorted in following way- YList-

[('4C:49:E3:64:9D:D6'    -63), ('00:EC:0A:87:61:F8'  -58),  
 ('CC:9F:7A:99:0D:EA'    -80), ('00:04:F2:FF:7A:1D'  -88)]

I am a beginner in python. How to copy the elements in the above format?

taras
  • 6,566
  • 10
  • 39
  • 50
C A Anusha
  • 37
  • 7

1 Answers1

0

Seems like you are trying to sort the list in descending order. If you need to keep the original list, you could do it using sorted() builtin function

xlist = [('00:04:F2:FF:7A:1D', -88), ('4C:49:E3:64:9D:D6', -63),
         ('00:EC:0A:87:61:F8', -58), ('CC:9F:7A:99:0D:EA', -80)]
ylist = sorted(xlist, key=lambda x: x[1], reverse=True)
print(ylist)

You also may sort the xlist inplace using list.sort() method:

xlist.sort(key=lambda x: x[1], reverse=True)
print(xlist)
leotrubach
  • 1,509
  • 12
  • 15
  • output:- [('00:EC:0A:87:61:F8', -58), ('4C:49:E3:64:9D:D6', -63), ('CC:9F:7A:99:0D:EA', -80), ('00:04:F2:FF:7A:1D', -88)] which is correct but in question wrongly given output as [('4C:49:E3:64:9D:D6' -63), ('00:EC:0A:87:61:F8' -58), ('CC:9F:7A:99:0D:EA' -80), ('00:04:F2:FF:7A:1D' -88)] need edit@C A Anusha – devesh Aug 20 '18 at 07:21