Home > Software engineering >  Sort the list of tuples in python
Sort the list of tuples in python

Time:12-20

I have tuples like this

[('AVAX', '070122'), ('AVAX', '201221'), ('AVAX', '211221'), ('AVAX', '241221'), ('AVAX', '311221'), ('BNB', '070122'), ('BNB', '201221'), ('BNB', '211221'), ('BNB', '241221'), ('BNB', '280122'), ('BNB', '311221'), ('BTC', '070122'), ('BTC', '201221'), ('BTC', '211221'), ('BTC', '241221'), ('BTC', '250222'), ('BTC', '250322'), ('BTC', '280122'), ('BTC', '311221'), ('ETH', '070122'), ('ETH', '201221'), ('ETH', '211221'), ('ETH', '241221'), ('ETH', '250222'), ('ETH', '250322'), ('ETH', '280122'), ('ETH', '311221'), ('MATIC', '070122'), ('MATIC', '201221'), ('MATIC', '211221'), ('MATIC', '241221'), ('MATIC', '311221'), ('SOL', '070122'), ('SOL', '201221'), ('SOL', '211221'), ('SOL', '241221'), ('SOL', '280122'), ('SOL', '311221')]

these are the coins and its expiries. The date is in string format, so the arrangement is wrong.

so i have changed the format to date and tried to arrange. I have tried the below code.

filtered_final_product_list = [list(ele) for ele in filtered_final_product_list]
new_list=list()
for i in filtered_final_product_list:
    i[1]=datetime.strptime(i[1],'%d%m%y')
    new_list.append(i)
    
print(sorted(new_list))

and the result is as expected as sorted.

[['AVAX', datetime.datetime(2021, 12, 20, 0, 0)], ['AVAX', datetime.datetime(2021, 12, 21, 0, 0)], ['AVAX', datetime.datetime(2021, 12, 24, 0, 0)], ['AVAX', datetime.datetime(2021, 12, 31, 0, 0)], ['AVAX', datetime.datetime(2022, 1, 7, 0, 0)], ['BNB', datetime.datetime(2021, 12, 20, 0, 0)], ['BNB', datetime.datetime(2021, 12, 21, 0, 0)], ['BNB', datetime.datetime(2021, 12, 24, 0, 0)], ['BNB', datetime.datetime(2021, 12, 31, 0, 0)], ['BNB', datetime.datetime(2022, 1, 7, 0, 0)], ['BNB', datetime.datetime(2022, 1, 28, 0, 0)], ['BTC', datetime.datetime(2021, 12, 20, 0, 0)], ['BTC', datetime.datetime(2021, 12, 21, 0, 0)], ['BTC', datetime.datetime(2021, 12, 24, 0, 0)], ['BTC', datetime.datetime(2021, 12, 31, 0, 0)], ['BTC', datetime.datetime(2022, 1, 7, 0, 0)], ['BTC', datetime.datetime(2022, 1, 28, 0, 0)], ['BTC', datetime.datetime(2022, 2, 25, 0, 0)], ['BTC', datetime.datetime(2022, 3, 25, 0, 0)], ['ETH', datetime.datetime(2021, 12, 20, 0, 0)], ['ETH', datetime.datetime(2021, 12, 21, 0, 0)], ['ETH', datetime.datetime(2021, 12, 24, 0, 0)], ['ETH', datetime.datetime(2021, 12, 31, 0, 0)], ['ETH', datetime.datetime(2022, 1, 7, 0, 0)], ['ETH', datetime.datetime(2022, 1, 28, 0, 0)], ['ETH', datetime.datetime(2022, 2, 25, 0, 0)], ['ETH', datetime.datetime(2022, 3, 25, 0, 0)], ['MATIC', datetime.datetime(2021, 12, 20, 0, 0)], ['MATIC', datetime.datetime(2021, 12, 21, 0, 0)], ['MATIC', datetime.datetime(2021, 12, 24, 0, 0)], ['MATIC', datetime.datetime(2021, 12, 31, 0, 0)], ['MATIC', datetime.datetime(2022, 1, 7, 0, 0)], ['SOL', datetime.datetime(2021, 12, 20, 0, 0)], ['SOL', datetime.datetime(2021, 12, 21, 0, 0)], ['SOL', datetime.datetime(2021, 12, 24, 0, 0)], ['SOL', datetime.datetime(2021, 12, 31, 0, 0)], ['SOL', datetime.datetime(2022, 1, 7, 0, 0)], ['SOL', datetime.datetime(2022, 1, 28, 0, 0)]]

now when i want back in older format which i mentioned in string, its not in the same order. It goes into unsorted way. Need help in making in sorted way in string format at the end.

CodePudding user response:

Sort by the coin and the date using datetime.datetime:

from datetime import datetime
sorted(lst, key=lambda x:(x[0], datetime.strptime(x[1], '%d%m%y')))

Output:

[('AVAX', '201221'), ('AVAX', '211221'), ('AVAX', '241221'), 
 ('AVAX', '311221'), ('AVAX', '070122'), ('BNB', '201221'), 
 ('BNB', '211221'), ('BNB', '241221'), ('BNB', '311221'), 
 ('BNB', '070122'), ('BNB', '280122'), ('BTC', '201221'),
 ('BTC', '211221'), ('BTC', '241221'), ('BTC', '311221'), 
 ('BTC', '070122'), ('BTC', '280122'), ('BTC', '250222'), 
 ('BTC', '250322'), ('ETH', '201221'), ('ETH', '211221'), 
 ('ETH', '241221'), ('ETH', '311221'), ('ETH', '070122'), 
 ('ETH', '280122'), ('ETH', '250222'), ('ETH', '250322'), 
 ('MATIC', '201221'), ('MATIC', '211221'), ('MATIC', '241221'), 
 ('MATIC', '311221'), ('MATIC', '070122'), ('SOL', '201221'),
 ('SOL', '211221'), ('SOL', '241221'), ('SOL', '311221'), 
 ('SOL', '070122'), ('SOL', '280122')]

CodePudding user response:

This will do what you ask, but as I said, it would be better for you to generate these tuples with the dates in the proper format to begin with.

product_list = [('AVAX', '070122'), ('AVAX', '201221'), ('AVAX', '211221'), ('AVAX', '241221'), ('AVAX', '311221'), ('BNB', '070122'), ('BNB', '201221'), ('BNB', '211221'), ('BNB', '241221'), ('BNB', '280122'), ('BNB', '311221'), ('BTC', '070122'), ('BTC', '201221'), ('BTC', '211221'), ('BTC', '241221'), ('BTC', '250222'), ('BTC', '250322'), ('BTC', '280122'), ('BTC', '311221'), ('ETH', '070122'), ('ETH', '201221'), ('ETH', '211221'), ('ETH', '241221'), ('ETH', '250222'), ('ETH', '250322'), ('ETH', '280122'), ('ETH', '311221'), ('MATIC', '070122'), ('MATIC', '201221'), ('MATIC', '211221'), ('MATIC', '241221'), ('MATIC', '311221'), ('SOL', '070122'), ('SOL', '201221'), ('SOL', '211221'), ('SOL', '241221'), ('SOL', '280122'), ('SOL', '311221')]

product_list.sort( key=lambda k: (k[0],k[1][4:6] k[1][2:4] k[1][0:2]))
print(product_list)

CodePudding user response:


When you want to create the new_list and want to have back up of existing list

Note : sorted() :-> creates a new list



borrowed code from @manlai A: thank you for sharing your answer

   from datetime import datetime
    new_list = sorted(lst, key=lambda x:(x[0], datetime.strptime(x[1], '%d%m%y')))
    print(new_list)

When you want to Modify Existing list

Note : given_list_obj.sort(key=function) :-> sort existing list


new_list1 = lst.copy()
new_list1.sort(key=lambda x:(x[0], datetime.strptime(x[1], '%d%m%y')))
print(new_list)

output

    [('AVAX', '201221'), ('AVAX', '211221'), ('AVAX', '241221'), 
 ('AVAX', '311221'), ('AVAX', '070122'), ('BNB', '201221'), 
 ('BNB', '211221'), ('BNB', '241221'), ('BNB', '311221'), 
 ('BNB', '070122'), ('BNB', '280122'), ('BTC', '201221'),
 ('BTC', '211221'), ('BTC', '241221'), ('BTC', '311221'), 
 ('BTC', '070122'), ('BTC', '280122'), ('BTC', '250222'), 
 ('BTC', '250322'), ('ETH', '201221'), ('ETH', '211221'), 
 ('ETH', '241221'), ('ETH', '311221'), ('ETH', '070122'), 
 ('ETH', '280122'), ('ETH', '250222'), ('ETH', '250322'), 
 ('MATIC', '201221'), ('MATIC', '211221'), ('MATIC', '241221'), 
 ('MATIC', '311221'), ('MATIC', '070122'), ('SOL', '201221'),
 ('SOL', '211221'), ('SOL', '241221'), ('SOL', '311221'), 
 ('SOL', '070122'), ('SOL', '280122')]
  • Related