Skip to content Skip to sidebar Skip to footer

Sorting A List Of Tuples Based On The First Items

How to sort a list of tuples based on the first value i.e, in a dictionary we can use sorted(a.keys()). How to do it for a list of tuples? If these are the tuple values t = [('201

Solution 1:

Or you can use something like this to be sure that list of tuples sorted by dates:

from datetime import datetime
initData = [('2010-09-11','somedata',1), ('2010-06-11','somedata',2), ('2010-09-12','somedata',3)]
outData = sorted(initData , key=lambda x: datetime.strptime(x[0],"%Y-%m-%d"))

Solution 2:

If '2010-09-11' is year-month-day , you do:

somedata = 'jyhghg'
t = [('2010-09-11','somedata',somedata),
     ('2010-06-11','somedata',somedata),
     ('2010-09-12','somedata',somedata),
     ('2010-08-12','somedata',somedata)]

from operator import itemgetter
t.sort(key = itemgetter(0))
print t

result

[('2010-06-11', 'somedata', 'jyhghg'),
 ('2010-08-12', 'somedata', 'jyhghg'),
 ('2010-09-11', 'somedata', 'jyhghg'),
 ('2010-09-12', 'somedata', 'jyhghg')]

.

If '2010-09-11' is year-day-month, you do:

from time import strptime,strftime

somedata = 'jyhghg'
t = [('2010-09-11','somedata',somedata),
     ('2010-06-11','somedata',somedata),
     ('2010-09-12','somedata',somedata),
     ('2010-08-12','somedata',somedata)]

t.sort(key = lambda x: strftime('%Y%m%d',strptime(x[0],'%Y-%d-%m')))
print t

result

[('2010-06-11', 'somedata', 'jyhghg'),
 ('2010-09-11', 'somedata', 'jyhghg'),
 ('2010-08-12', 'somedata', 'jyhghg'),
 ('2010-09-12', 'somedata', 'jyhghg')]

.

Edit 1

Reading the answer of Artsiom Rudzenka in which he uses strptime() alone, I realized that strptime() produces a struct_time object that is sorted by nature . Such an object has attributes tm_year, tm_mon, tm_mday, tm_hour, tm_min, tm_sec, tm_wday, tm_yday, tm_isdst that are accessible through common dot-notation access (toto.tm_mon for exemple), but also through index-notation access (toto[1] for exemple) , because the attributes of a struc_time object are registered in this order tm_year, tm_mon, tm_mday, tm_hour, tm_min, tm_sec, tm_wday, tm_yday, tm_isdst . The struct_time data type has a named tuple's interface .

Since a struct_time object is ordered by nature, it isn't necessary to apply strftime() to obtain a date string having year-month-day in this order: this order is already present in the struct_time object.

Then , I correct my code for the case in which 11 in '2010-06-11' is the month : I eliminate strftime()

from time import strptime

somedata = 'jyhghg'
t = [('2010-09-11','somedata',somedata),
     ('2010-06-11','somedata',somedata),
     ('2010-09-12','somedata',somedata),
     ('2010-08-12','somedata',somedata)]

t.sort(key = lambda x: strptime(x[0],'%Y-%d-%m'))
print t

Edit 2

Taking Kirk Strauser's info in consideration:

import re

regx = re.compile('(\d{4})-(\d\d)-(\d\d)')

somedata = 'jyhghg'
t = [('2010-09-11','somedata',somedata),
     ('2010-06-11','somedata',somedata),
     ('2010-09-12','somedata',somedata),
     ('2010-08-12','somedata',somedata)]

t.sort(key = lambda x: regx.match(x[0]).group(1,3,2))
print t

Solution 3:

You can use the very simple

t.sort()

see: How does Python sort a list of tuples?

Post a Comment for "Sorting A List Of Tuples Based On The First Items"