Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

sort python nested list according to a value

name_age = [["A", 7], ["B", 5], ["C", 35]]
name_age.sort(key=lambda age: age[1])
#name_age.sort(key=lambda age: age[1], reverse = True)
print(name_age)

#sorts according to the 1th value of the inner loop
Comment

how to sort nested list in python

# Python code to sort the lists using the second element of sublists
# Inplace way to sort, use of third variable
def Sort(sub_li):
    l = len(sub_li)
    for i in range(0, l):
        for j in range(0, l-i-1):
            if (sub_li[j][1] > sub_li[j + 1][1]):
                tempo = sub_li[j]
                sub_li[j]= sub_li[j + 1]
                sub_li[j + 1]= tempo
    return sub_li
  
# Driver Code
sub_li =[['rishav', 10], ['akash', 5], ['ram', 20], ['gaurav', 15]]
print(Sort(sub_li))
Comment

PREVIOUS NEXT
Code Example
Python :: pd.explode 
Python :: python check if character in string 
Python :: numpy split 
Python :: what is python 
Python :: resampling data python 
Python :: python ascii art 
Python :: pandas columns 
Python :: if it is square python 
Python :: how to read mysql table in python 
Python :: filter json python 
Python :: how to duplicate a list in python 
Python :: jama api python 
Python :: array and list in python 
Python :: Simple example of python strip function 
Python :: count python 
Python :: Pass a variable to dplyr "rename" to change columnname 
Python :: set default dictionary in python 
Python :: abstract class in python 
Python :: partition python 
Python :: df.info() in python 
Python :: print numbers from 1 to 100 in python 
Python :: how to open chrome console in selenium webdriver 
Python :: python discord bot embed 
Python :: 2d array python initialize 
Python :: csv to pdf python 
Python :: python all any example 
Python :: error aesthetics must be either length 1 or the same as the data (3) fill 
Python :: duck typing in python 
Python :: how to open link in new tab selenium python 
Python :: File "main.py", line 21 print("total harga:idr", bakso bulat +str Minuman Drink): ^ SyntaxError: invalid syntax 
ADD CONTENT
Topic
Content
Source link
Name
6+4 =