Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

arrange the list of tuples python

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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')]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading