{"id":3647,"date":"2023-10-23T07:55:35","date_gmt":"2023-10-23T02:25:35","guid":{"rendered":"https:\/\/python-programs.com\/?p=3647"},"modified":"2023-11-10T11:56:01","modified_gmt":"2023-11-10T06:26:01","slug":"python-how-to-sort-a-list-of-strings-list-sort","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-how-to-sort-a-list-of-strings-list-sort\/","title":{"rendered":"Python : How to Sort a list of strings ? | list.sort()"},"content":{"rendered":"
In this article we will discuss about 4 different ways of sorting a list of strings.<\/p>\n
In python, list is a versatile data type which stores multiple elements in a single variable without caring about the types of data.<\/p>\n
Syntax : my_list = [element1, element2, element3]<\/pre>\nThe 4 ways are<\/p>\n
\n
- By Alphabetical Order<\/li>\n
- By Reverse Alphabetical Order<\/li>\n
- By String Length<\/li>\n
- By Numeric Order<\/li>\n<\/ol>\n
List provides a
sort()<\/code> method by using which we can sort the elements of list from lower order to higher order.<\/p>\n
So, let’s start exploring the topic.<\/p>\n
Method-1 : By Alphabetical Order<\/h3>\n
Let’s see a program how it sorts in alphabetical order.<\/p>\n
#Program :\r\n\r\n#List Of Strings\r\nmy_list = ['bat' , 'ant', 'cat', 'eye', 'dog']\r\nmy_list.sort()\r\nprint(my_list)\r\n<\/pre>\nOutput :\r\n['ant' , 'bat', 'cat', 'dog', 'eye']<\/pre>\nMethod-2 : By Reverse Alphabetical Order<\/h3>\n
Let’s see a program how it sorts in alphabetical order.<\/p>\n
#Program :\r\n\r\n#List Of Strings\r\nmy_list = ['bat' , 'ant', 'cat', 'eye', 'dog']\r\nmy_list.sort(reverse=True)\r\nprint(my_list)\r\n<\/pre>\nOutput :\r\n['eye' , 'do', 'cat', 'bat', 'ant']<\/pre>\nHere, sort() method accepts another argument reverse, and it it is True then it sorts the element. Default value is False. When set True, it sorts element in reverse order.<\/p>\n
Method-3 : By String Length<\/h3>\n
Let’s see a program how it sorts in alphabetical order.<\/p>\n
#Program :\r\n\r\n#List Of Strings\r\nmy_list = ['apple' , 'ant', 'aeroplane', 'auto', 'a']\r\nmy_list.sort(key=len)\r\nprint(my_list)\r\n<\/pre>\nOutput :\r\n['a' , 'ant', 'auto', 'apple', 'aeroplane']<\/pre>\nHere, sort() method accepts another argument i.e key. During sorting each element of list will be compared with each other.<\/p>\n
Syntax : list.sort(key=function)<\/pre>\nHere we have passed
len()<\/code> function as key function. And all the element are sorted as per string length.<\/p>\n
Method-4 : By Numeric Order<\/h3>\n
Suppose we have a list of strings, where each element are of numbers. There we can use int() as key function.<\/p>\n
Let’s see a program how it sorts in alphabetical order.<\/p>\n
#Program : \r\n\r\n#List Of Strings \r\nmy_list = ['4' , '6', '1', '3', '2'] \r\nmy_list.sort(key=int) \r\nprint(my_list)\r\n<\/pre>\nOutput :\r\n['1','2','3','4','6']<\/pre>\n","protected":false},"excerpt":{"rendered":"How to sort a list of strings using list.sort() in python ? In this article we will discuss about 4 different ways of sorting a list of strings. In python, list is a versatile data type which stores multiple elements in a single variable without caring about the types of data. Syntax : my_list = …<\/p>\n