{"id":8623,"date":"2023-11-04T11:09:36","date_gmt":"2023-11-04T05:39:36","guid":{"rendered":"https:\/\/python-programs.com\/?p=8623"},"modified":"2023-11-10T12:17:13","modified_gmt":"2023-11-10T06:47:13","slug":"python-program-to-sort-a-list-according-to-the-length-of-the-elements","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-sort-a-list-according-to-the-length-of-the-elements\/","title":{"rendered":"Python Program to Sort a List According to the Length of the Elements"},"content":{"rendered":"
List in Python :<\/strong><\/p>\n The list data type is one of the most often used data types in Python. The square brackets [ ] easily identify a Python List. Lists are used to store data items, with each item separated by a comma (,). A Python List can include data elements of any data type, including integers and Booleans.<\/p>\n One of the primary reasons that lists are so popular is that they are mutable. Any data item in a List can be replaced by any other data item if it is mutable. This distinguishes Lists from Tuples, which are likewise used to store data elements but are immutable.<\/p>\n Given a list, the task is to sort the list according to the length of the elements in the given list<\/p>\n Example:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n There are several ways to sort the given list according to the length of the elements some of them are:<\/p>\n Drive into Python Programming Examples<\/a> and explore more instances related to python concepts so that you can become proficient in generating programs in Python Programming Language.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n List in Python : The list data type is one of the most often used data types in Python. The square brackets [ ] easily identify a Python List. Lists are used to store data items, with each item separated by a comma (,). A Python List can include data elements of any data type, …<\/p>\ngiven list = [\"hello\", \"this\", \"is\", \"BTechGeeks\", \"online\", \"platform\", \"for\", \"Python\", \"and\", \"many\", \"Programming\", \"languages\"]<\/pre>\n
printing the given list before sorting according to length : \r\n['hello', 'this', 'is', 'BTechGeeks', 'online', 'platform', 'for', 'Python', 'and', 'many', 'Programming', 'languages']\r\nprinting the given list after sorting according to length : \r\n['is', 'for', 'and', 'this', 'many', 'hello', 'online', 'Python', 'platform', 'languages', 'BTechGeeks', 'Programming']<\/pre>\n
Python Program to Sort a List According to the Length of the Elements<\/h2>\n
\n
Method #1:Using sort function ( Static Input )<\/h3>\n
\n
def lengthSort(given_list):\r\n # sorting the given list by length of elements of the given list\r\n given_list.sort(key=len)\r\n # return the list\r\n return given_list\r\n\r\n\r\n# given list\r\ngiven_list = [\"hello\", \"this\", \"is\", \"BTechGeeks\", \"online\", \"platform\",\r\n \"for\", \"Python\", \"and\", \"many\", \"Programming\", \"languages\"]\r\n# printing the given list before sorting according to length\r\nprint(\"printing the given list before sorting according to length : \")\r\nprint(given_list)\r\n# passing the given_list to lengthSort function to sort\r\n# the list according to the length of the elements of the given list\r\n# printing the given list before sorting according to length\r\nprint(\"printing the given list after sorting according to length : \")\r\nprint(lengthSort(given_list))\r\n<\/pre>\n
printing the given list before sorting according to length : \r\n['hello', 'this', 'is', 'BTechGeeks', 'online', 'platform', 'for', 'Python', 'and', 'many', 'Programming', 'languages']\r\nprinting the given list after sorting according to length : \r\n['is', 'for', 'and', 'this', 'many', 'hello', 'online', 'Python', 'platform', 'languages', 'BTechGeeks', 'Programming']<\/pre>\n
Method #2:Using sort function ( User Input )<\/h3>\n
\n
def lengthSort(given_list):\r\n # sorting the given list by length of elements of the given list\r\n given_list.sort(key=len)\r\n # return the list\r\n return given_list\r\n\r\n\r\n# given list\r\ngiven_list = list(\r\n input(\"Enter the elements of the given list separated by spaces = \").split())\r\n# printing the given list before sorting according to length\r\nprint(\"printing the given list before sorting according to length : \")\r\nprint(given_list)\r\n# passing the given_list to lengthSort function to sort\r\n# the list according to the length of the elements of the given list\r\n# printing the given list before sorting according to length\r\nprint(\"printing the given list after sorting according to length : \")\r\nprint(lengthSort(given_list))\r\n<\/pre>\n
Enter the elements of the given list separated by spaces =hello this is BTechGeeks online platform for Python and many Programming languages \r\nprinting the given list before sorting according to length : \r\n['hello', 'this', 'is', 'BTechGeeks', 'online', 'platform', 'for', 'Python', 'and', 'many', 'Programming', 'languages']\r\nprinting the given list after sorting according to length : \r\n['is', 'for', 'and', 'this', 'many', 'hello', 'online', 'Python', 'platform', 'languages', 'BTechGeeks', 'Programming']<\/pre>\n
Method #3:Using sorted function ( Static Input )<\/h3>\n
\n
def lengthSort(given_list):\r\n # sorting the given list by length of elements of the given list\r\n given_list = sorted(given_list, key=len)\r\n # return the list\r\n return given_list\r\n\r\n\r\n# given list\r\ngiven_list = [\"hello\", \"this\", \"is\", \"BTechGeeks\", \"online\", \"platform\",\r\n \"for\", \"Python\", \"and\", \"many\", \"Programming\", \"languages\"]\r\n# printing the given list before sorting according to length\r\nprint(\"printing the given list before sorting according to length : \")\r\nprint(given_list)\r\n# passing the given_list to lengthSort function to sort\r\n# the list according to the length of the elements of the given list\r\n# printing the given list before sorting according to length\r\nprint(\"printing the given list after sorting according to length : \")\r\nprint(lengthSort(given_list))\r\n<\/pre>\n
printing the given list before sorting according to length : \r\n['hello', 'this', 'is', 'BTechGeeks', 'online', 'platform', 'for', 'Python', 'and', 'many', 'Programming', 'languages']\r\nprinting the given list after sorting according to length : \r\n['is', 'for', 'and', 'this', 'many', 'hello', 'online', 'Python', 'platform', 'languages', 'BTechGeeks', 'Programming']<\/pre>\n
Method #4:Using sorted function ( User Input )<\/h3>\n
\n
def lengthSort(given_list):\r\n # sorting the given list by length of elements of the given list\r\n given_list = sorted(given_list, key=len)\r\n # return the list\r\n return given_list\r\n\r\n\r\n# given list\r\ngiven_list = list(\r\n input(\"Enter the elements of the given list separated by spaces\").split())\r\n# printing the given list before sorting according to length\r\nprint(\"printing the given list before sorting according to length : \")\r\nprint(given_list)\r\n# passing the given_list to lengthSort function to sort\r\n# the list according to the length of the elements of the given list\r\n# printing the given list before sorting according to length\r\nprint(\"printing the given list after sorting according to length : \")\r\nprint(lengthSort(given_list))\r\n<\/pre>\n
Enter the elements of the given list separated by spaces =hello this is BTechGeeks online platform for Python and many Programming languages \r\nprinting the given list before sorting according to length : \r\n['hello', 'this', 'is', 'BTechGeeks', 'online', 'platform', 'for', 'Python', 'and', 'many', 'Programming', 'languages']\r\nprinting the given list after sorting according to length : \r\n['is', 'for', 'and', 'this', 'many', 'hello', 'online', 'Python', 'platform', 'languages', 'BTechGeeks', 'Programming']<\/pre>\n
\n