python sort function | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

python sort function

sub_li =[['rishav', 10], ['akash', 5], ['ram', 20], ['gaurav', 15]] a=sub_li.sort(key = lambda x: x[1]) print(a) why am i getting none as a output not the sorted list why am i getting none when i should get the sorted list

30th Jun 2019, 8:42 AM
I Am Anushka
2 Answers
+ 11
Because the sort() function does not work like this. It actually changes your original list (in-place sorting). So you can do like: sub_li.sort() print(sub_li) Or: a = sorted(sub_li) print(a) You can use the key parameter with both ways
30th Jun 2019, 8:49 AM
Tibor Santa
Tibor Santa - avatar
+ 4
Thanks
30th Jun 2019, 8:52 AM
I Am Anushka