Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Sorting Tuple/List In Python

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 857
    Comment on it

    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 

    1. 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).

    1. sorted_by_first = sorted(data, key=lambda tup: tup[0])
    2. for x in sorted_by_first:
    3. print(x)

    if you run above code you get:-

    1. [1, 10, 3]
    2. [4, 5, 6]
    3. [7, 8, 9]

    If you want to sort in descending order :-

    1. sorted_by_First = sorted(data, key=lambda tup: tup[0] , reverse = True)
    2. for x in sorted_by_First:
    3. print(x)

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: