Hi All,
In programming world, we have to perform some basic operation on list or tuple but in python , it is easy to perform any operation on list or tuple. for example:-
If you have a number tuple/list like
data = [[1,10,8], [4,5,6], [7,8,9]]
then you can sort this list by any row like below code , in which data is sorted in ascending order ( for row 1).
sorted_by_first = sorted(data, key=lambda tup: tup[0])
for x in sorted_by_first:
print(x)
if you run above code you get:-
[1, 10, 3]
[4, 5, 6]
[7, 8, 9]
If you want to sort in descending order :-
sorted_by_First = sorted(data, key=lambda tup: tup[0] , reverse = True)
for x in sorted_by_First:
print(x)
0 Comment(s)