Untitled Document
Sorting - List, Tuples and Objects
1. Introduction to sorting Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****   
       # Introduction to sorting
       # Here we will see two methods available for sorting list, tupples and Objects 
       # 1. Sort Method and 2. Sorted Method
       # Sets are unordered list so we can not do sorting on it 
       # Tuples are immutable so only sorted method is applicable on tuple
       # sort - method will actually sort and make changes in original list, dictionary or ojects 
       # sorted - method will return sorted version of list, dictionary and objects without changing original list, dictionary and objects
       # we can store the sorted method output in a variable for future reference 
    
       
2. Sorting List using sort method Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Sorting List using sort method
       # IMP - always use sort function function separatetly e.g print(.sort) will give None as output
       # Hence - always follow the convention mentioned in below example 
       
       # EX - 1 
       
       li = [9, 7, 4, 1, 3, 2, 8, 6, 5]
       li.sort()
       print(li)
       
       # EX - 2 Sorting list in descending order
       
       li = [9, 7, 4, 1, 3, 2, 8, 6, 5]
       li.sort(reverse=True)
       print(li)
       
3. Sorting Tuple using sorted method Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Sorting Tuple using sorted method 
       # IMP - Sorting tuple ; since tuple is immutable you can't run sort method on it ; instead you can use sorted method
             
       my_tup = [9, 7, 4, 1, 3, 2, 8, 6, 5]
       tup_sorted = sorted(my_tup)
       print(tup_sorted)
       
       
4. Sorting List using sorted method Click Here
       #********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
       # Sorting List using sorted method  
      
       # EX - 1  Simple Example of sorted function   
           
       li = [-8 ,9,6 , 1, 2, 3]
       sorted_li = sorted(li)
       print(sorted_li)
       
       # EX - 2 One more option i.e key parameer with sorted function ; see the following example
       
       li = [-6, -5, -4 , 1, 2, 3]
       sorted_li = sorted(li , key=abs)
       print(sorted_li)
       
Untitled Document