Skip to main content

Posts

Showing posts from September, 2023

Different Ways of Sorting Data in a List - Python

Sorting Data in a List List is a collection of data (of different data types), much like an array. Like any data structure or data set, data in the list doesn't necessarily be in any specific order.  At times, depending on the use case we get to order the data in a specific sequence (Ascending, Descending or Reverse). There are couple of methods and functions for sorting the data in a list, which can be used depending on the use case.  sort() method  sorted() function reverse() method reversed() function  Let's have a look at each of these in detail.  sort() method Sort() method by default sorts the data in an ascending order. One important thing to remember about sort() method is that, it sorts the list in-place, meaning the list on which method is called would be sorted and the method would return None (no sorted list would be returned).  Syntax           list.sort(<key=None>, <reverse=False>) The two parameters key and reverse mentioned in the syntax are optiona