another way to sort like l.sort(key=lambda x:(x[0][0], -x[1][0]))

sajuptpm sajuptpm at gmail.com
Tue Sep 7 09:24:06 EDT 2010


I have a list of tuples.

l = [((30,50),(70)), ((50,20),(20))]

for i in range(10):
	k = ((i+30,i+50),(i+70))#suppose creating new tuple in each iteration
using some random value and in sert it into list.

	flag=True
	for i, v in enumerate(l):
	if v >= k:
	        l.insert(i,k)
	        flag = False
	        break
	if flag:
	l.append(k)


This code will give a list of tuples sorted in asc order.
I need to change this code to sort this list by k[0][0] ascending and
k[0][1] descending.
What are the modifications needed in this code.
I dont want to user sort() method of list.

i need to implement  l.sort(key=lambda x:(x[0][0], -x[1][0])) in
another way .I want to know what the modification needed in the 'if'
check to sort this list of tuples in k[0][0] ascending and k[0][1]
descending.



More information about the Python-list mailing list