Table of Contents
The task is to sort the given list of elements by second item. In this article we will discuss different ways of sorting in python.
Approach 1: sort() method
Approach 2: sorted() method
Approach 3: bubble sort
We have used three approaches to sort the elements by second element. The first approach is using sort() method. Python provides the in-built function sort() to sort the elements of list. When we use this method, the actual list values are changed and get sorted.
The second approach is using python in-built sorted() method. This method does not change the original list, instead just returns the sorted value.
The third and last approach is using bubble sort algorithm. From code we can see that the second value of list is passed in for loop and sorted accordingly.