How to sort a list of lists in python

In this tutorial we will learn about How to sort a list of lists in python. We can sort a list  by arranging the elements in ascending or descending order based on the requirement. We can set the list by both the order.And you can also set a list by its alphabet number or index number also. So lets learn this guys by given methods. I hope you all like it.

How to sort a list of lists in python

Advertisements
  1. sort a list of lists in python

    to sort a list of lists in python just Use itemgetter() and sorted(). itemgetter() and sorted() are used for to sort the list in python. You can sort a list by its index number or also alphabet number. Here we used two list to sorted it out. List1 is sort at the basis of index number and the list2 is sorted by its alphabet number by using itemgetter() and sorted() functions. itemgetter() takes an index number as a parameter and returns the element based on its index number. Thus we can sort a list. Now lets learn by an example: list1 = [[40,'maths'], [45,'science'], [49,'english']] print("Sorted List on index 0: % s" % (sorted(list1, key=lambda x:x[0]))) list2 = [[4,'pizza'], [2, 'Burger'], [7, 'dhosa']] print("Sorted List on index 1: % s" % (sorted(list2, key=lambda x:x[1])))
    Output : Sorted List on index 0: [[40, 'maths'], [45, 'science'], [49, 'english']] Sorted List on index 1: [[2, 'Burger'], [7, 'dhosa'], [4, 'pizza']]

  2. How to sort a list of lists in python

    to sort a list of lists in python just Use sort(). By using sort() you can sort a list in python. sort() function use for set the list according to the first element of the list and it cal also reverce the list according to the first element of the list. This method is very easy and widely using among of all these methods. So lets learn this by an example. list1 = [[12,15], [45,54], [52,15]] list1.sort() print("sorted list: % s" % (list1)) list1.sort(reverse=True) print("reverse sorted list : % s" % (list1)) Output : sorted list: [[12, 15], [45, 54], [52, 15]] reverse sorted list : [[52, 15], [45, 54], [12, 15]]

Method 1 : Use itemgetter() and sorted()

Advertisements

itemgetter() and sorted() are used for to sort the list in python. You can sort a list by its index number or also alphabet number. Here we used two list to sorted it out. List1 is sort at the basis of index number and the list2 is sorted by its alphabet number by using itemgetter() and sorted() functions. itemgetter() takes an index number as a parameter and returns the element based on its index number. Thus we can sort a list. Now lets learn by an example:

list1 = [[40,'maths'], [45,'science'], [49,'english']]
print("Sorted List on index 0: % s" % (sorted(list1, key=lambda x:x[0])))
list2 = [[4,'pizza'], [2, 'Burger'], [7, 'dhosa']]
print("Sorted List on index 1: % s" % (sorted(list2, key=lambda x:x[1])))

Output :

Sorted List on index 0: [[40, 'maths'], [45, 'science'], [49, 'english']]
Sorted List on index 1: [[2, 'Burger'], [7, 'dhosa'], [4, 'pizza']]

Method 2: Use sort()

Advertisements

By using sort() you can sort a list in python. sort() function use for set the list according to the first element of the list and it cal also reverce the list according to the first element of the list. This method is very easy and widely using among of all these methods. So lets learn this by given below example. I hope you like it.

list1 = [[12,15], [45,54], [52,15]]
list1.sort()
print("sorted list: % s" % (list1))
list1.sort(reverse=True)
print("reverse sorted list : % s" % (list1))

Output :

sorted list: [[12, 15], [45, 54], [52, 15]]
reverse sorted list : [[52, 15], [45, 54], [12, 15]]

Method 3: Use key=len

Advertisements

key=len is use for to sort the list on bases of the length of the inner list. Thus it gives the smallest list first. so lets learn about of this by given below example:

list1 = [[12,15,18], [19], [51,52]]
list1.sort(key=len)
print("New sorted list is % s" % (list1))

Output :

New sorted list is [[19], [51, 52], [12, 15, 18]]

Conclusion

Advertisements

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment