{"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":"

How to sort a list of strings using list.sort() in python ?<\/h2>\n

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>\n

The 4 ways are<\/p>\n

    \n
  1. By Alphabetical Order<\/li>\n
  2. By Reverse Alphabetical Order<\/li>\n
  3. By String Length<\/li>\n
  4. 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>\n
    Output :\r\n['ant' , 'bat', 'cat', 'dog', 'eye']<\/pre>\n

    Method-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>\n
    Output :\r\n['eye' , 'do', 'cat', 'bat', 'ant']<\/pre>\n

    Here, 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>\n
    Output :\r\n['a' , 'ant',  'auto', 'apple', 'aeroplane']<\/pre>\n

    Here, 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>\n

    Here 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>\n
    Output :\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

    Python : How to Sort a list of strings ? | list.sort()<\/span> Read More »<\/a><\/p>\n","protected":false},"author":9,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[5],"tags":[],"yoast_head":"\nPython : How to Sort a list of strings ? | list.sort() - Python Programs<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/python-programs.com\/python-how-to-sort-a-list-of-strings-list-sort\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python : How to Sort a list of strings ? | list.sort() - Python Programs\" \/>\n<meta property=\"og:description\" content=\"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 = … Python : How to Sort a list of strings ? | list.sort() Read More »\" \/>\n<meta property=\"og:url\" content=\"https:\/\/python-programs.com\/python-how-to-sort-a-list-of-strings-list-sort\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Programs\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/btechgeeks\" \/>\n<meta property=\"article:published_time\" content=\"2023-10-23T02:25:35+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-11-10T06:26:01+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@btech_geeks\" \/>\n<meta name=\"twitter:site\" content=\"@btech_geeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Satyabrata Jena\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Organization\",\"@id\":\"https:\/\/python-programs.com\/#organization\",\"name\":\"BTech Geeks\",\"url\":\"https:\/\/python-programs.com\/\",\"sameAs\":[\"https:\/\/www.instagram.com\/btechgeeks\/\",\"https:\/\/www.linkedin.com\/in\/btechgeeks\",\"https:\/\/in.pinterest.com\/btechgeek\/\",\"https:\/\/www.youtube.com\/channel\/UC9MlCqdJ3lKqz2p5114SDIg\",\"https:\/\/www.facebook.com\/btechgeeks\",\"https:\/\/twitter.com\/btech_geeks\"],\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png\",\"contentUrl\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png\",\"width\":350,\"height\":70,\"caption\":\"BTech Geeks\"},\"image\":{\"@id\":\"https:\/\/python-programs.com\/#\/schema\/logo\/image\/\"}},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/python-programs.com\/#website\",\"url\":\"https:\/\/python-programs.com\/\",\"name\":\"Python Programs\",\"description\":\"Python Programs with Examples, How To Guides on Python\",\"publisher\":{\"@id\":\"https:\/\/python-programs.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/python-programs.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/python-programs.com\/python-how-to-sort-a-list-of-strings-list-sort\/#webpage\",\"url\":\"https:\/\/python-programs.com\/python-how-to-sort-a-list-of-strings-list-sort\/\",\"name\":\"Python : How to Sort a list of strings ? | list.sort() - Python Programs\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/#website\"},\"datePublished\":\"2023-10-23T02:25:35+00:00\",\"dateModified\":\"2023-11-10T06:26:01+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/python-programs.com\/python-how-to-sort-a-list-of-strings-list-sort\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/python-programs.com\/python-how-to-sort-a-list-of-strings-list-sort\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/python-programs.com\/python-how-to-sort-a-list-of-strings-list-sort\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/python-programs.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python : How to Sort a list of strings ? | list.sort()\"}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/python-programs.com\/python-how-to-sort-a-list-of-strings-list-sort\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/python-how-to-sort-a-list-of-strings-list-sort\/#webpage\"},\"author\":{\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/0f6d731bda7051ce3586f71299f391cd\"},\"headline\":\"Python : How to Sort a list of strings ? | list.sort()\",\"datePublished\":\"2023-10-23T02:25:35+00:00\",\"dateModified\":\"2023-11-10T06:26:01+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/python-programs.com\/python-how-to-sort-a-list-of-strings-list-sort\/#webpage\"},\"wordCount\":249,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/python-programs.com\/#organization\"},\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/python-programs.com\/python-how-to-sort-a-list-of-strings-list-sort\/#respond\"]}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/0f6d731bda7051ce3586f71299f391cd\",\"name\":\"Satyabrata Jena\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/c1bf8033ec357d085f41815bee1625cc?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/c1bf8033ec357d085f41815bee1625cc?s=96&d=mm&r=g\",\"caption\":\"Satyabrata Jena\"},\"url\":\"https:\/\/python-programs.com\/author\/satyabrata\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python : How to Sort a list of strings ? | list.sort() - Python Programs","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/python-programs.com\/python-how-to-sort-a-list-of-strings-list-sort\/","og_locale":"en_US","og_type":"article","og_title":"Python : How to Sort a list of strings ? | list.sort() - Python Programs","og_description":"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 = … Python : How to Sort a list of strings ? | list.sort() Read More »","og_url":"https:\/\/python-programs.com\/python-how-to-sort-a-list-of-strings-list-sort\/","og_site_name":"Python Programs","article_publisher":"https:\/\/www.facebook.com\/btechgeeks","article_published_time":"2023-10-23T02:25:35+00:00","article_modified_time":"2023-11-10T06:26:01+00:00","twitter_card":"summary_large_image","twitter_creator":"@btech_geeks","twitter_site":"@btech_geeks","twitter_misc":{"Written by":"Satyabrata Jena","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Organization","@id":"https:\/\/python-programs.com\/#organization","name":"BTech Geeks","url":"https:\/\/python-programs.com\/","sameAs":["https:\/\/www.instagram.com\/btechgeeks\/","https:\/\/www.linkedin.com\/in\/btechgeeks","https:\/\/in.pinterest.com\/btechgeek\/","https:\/\/www.youtube.com\/channel\/UC9MlCqdJ3lKqz2p5114SDIg","https:\/\/www.facebook.com\/btechgeeks","https:\/\/twitter.com\/btech_geeks"],"logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/python-programs.com\/#\/schema\/logo\/image\/","url":"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png","contentUrl":"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png","width":350,"height":70,"caption":"BTech Geeks"},"image":{"@id":"https:\/\/python-programs.com\/#\/schema\/logo\/image\/"}},{"@type":"WebSite","@id":"https:\/\/python-programs.com\/#website","url":"https:\/\/python-programs.com\/","name":"Python Programs","description":"Python Programs with Examples, How To Guides on Python","publisher":{"@id":"https:\/\/python-programs.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/python-programs.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/python-programs.com\/python-how-to-sort-a-list-of-strings-list-sort\/#webpage","url":"https:\/\/python-programs.com\/python-how-to-sort-a-list-of-strings-list-sort\/","name":"Python : How to Sort a list of strings ? | list.sort() - Python Programs","isPartOf":{"@id":"https:\/\/python-programs.com\/#website"},"datePublished":"2023-10-23T02:25:35+00:00","dateModified":"2023-11-10T06:26:01+00:00","breadcrumb":{"@id":"https:\/\/python-programs.com\/python-how-to-sort-a-list-of-strings-list-sort\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/python-programs.com\/python-how-to-sort-a-list-of-strings-list-sort\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/python-programs.com\/python-how-to-sort-a-list-of-strings-list-sort\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/python-programs.com\/"},{"@type":"ListItem","position":2,"name":"Python : How to Sort a list of strings ? | list.sort()"}]},{"@type":"Article","@id":"https:\/\/python-programs.com\/python-how-to-sort-a-list-of-strings-list-sort\/#article","isPartOf":{"@id":"https:\/\/python-programs.com\/python-how-to-sort-a-list-of-strings-list-sort\/#webpage"},"author":{"@id":"https:\/\/python-programs.com\/#\/schema\/person\/0f6d731bda7051ce3586f71299f391cd"},"headline":"Python : How to Sort a list of strings ? | list.sort()","datePublished":"2023-10-23T02:25:35+00:00","dateModified":"2023-11-10T06:26:01+00:00","mainEntityOfPage":{"@id":"https:\/\/python-programs.com\/python-how-to-sort-a-list-of-strings-list-sort\/#webpage"},"wordCount":249,"commentCount":0,"publisher":{"@id":"https:\/\/python-programs.com\/#organization"},"articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/python-programs.com\/python-how-to-sort-a-list-of-strings-list-sort\/#respond"]}]},{"@type":"Person","@id":"https:\/\/python-programs.com\/#\/schema\/person\/0f6d731bda7051ce3586f71299f391cd","name":"Satyabrata Jena","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/python-programs.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/c1bf8033ec357d085f41815bee1625cc?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c1bf8033ec357d085f41815bee1625cc?s=96&d=mm&r=g","caption":"Satyabrata Jena"},"url":"https:\/\/python-programs.com\/author\/satyabrata\/"}]}},"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/3647"}],"collection":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/comments?post=3647"}],"version-history":[{"count":2,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/3647\/revisions"}],"predecessor-version":[{"id":3652,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/3647\/revisions\/3652"}],"wp:attachment":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/media?parent=3647"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/categories?post=3647"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/tags?post=3647"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}