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 :: python number of cpus 
Python :: get current date and time with python 
Python :: iterate over df 
Python :: clear multiprocessing queue python 
Python :: list files in directory python with extension 
Python :: finding duplicate characters in a string python 
Python :: pandas shuffle rows 
Python :: pyspark import f 
Python :: classification report scikit 
Python :: folium anaconda 
Python :: load model keras 
Python :: how to install numpy 
Python :: how to check datatype of column in dataframe python 
Python :: matplotlib plot title font size 
Python :: how to check for a particular word in a text file using python 
Python :: how to remember to put a semicolon after your code 
Python :: how to limit a command to a permission in discord.py 
Python :: numpy read image 
Python :: pandas insert column in the beginning 
Python :: normalize values between 0 and 1 python 
Python :: create an array from 1 to n python 
Python :: SettingWithCopyWarning 
Python :: tqdm for jupyter notebook 
Python :: werkzeug.datastructures.filestorage to numpy 
Python :: python datetime now only hour and minute 
Python :: A value is trying to be set on a copy of a slice from a DataFrame. 
Python :: pandas.core.indexes.base.index to list 
Python :: check cuda version pytorch 
Python :: how to draw image in tkinter 
Python :: python word cloud 
ADD CONTENT
Topic
Content
Source link
Name
3+6 =