{"id":550,"date":"2023-02-21T15:06:43","date_gmt":"2023-02-21T15:06:43","guid":{"rendered":"https:\/\/tinyytopic.com\/?p=550"},"modified":"2023-02-21T15:05:56","modified_gmt":"2023-02-21T15:05:56","slug":"how-to-sort-the-python-list-with-an-index","status":"publish","type":"post","link":"https:\/\/tinyytopic.com\/index.php\/2023\/02\/21\/how-to-sort-the-python-list-with-an-index\/","title":{"rendered":"How to sort the Python list with an index"},"content":{"rendered":"\n<div class=\"wp-block-uagb-advanced-heading uagb-block-90038323\"><h5 class=\"uagb-heading-text\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"><br>How to sort the Python list with an index using Ready-to-use function?<\/mark><\/h5><\/div>\n\n\n\n<p style=\"font-size:15px\">Ready-to-use Python function to sort the list with an index:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"atomic\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">def sort_list_with_index(li1, idx):\n    # Sort a List with given index\n    tmp = li1[:] #clone list\n\n    # verify list and index size\n    if len(li1) != len(idx):\n        messagebox.showinfo('Message', \"The sorting list and index size doesn't match!\")\n        return\n    \n    # sort list based on index number\n    for i in range(0, len(idx)):\n        tmp[i] = li1[idx[i]]\n    \n    return tmp<\/pre>\n\n\n\n<p style=\"font-size:15px\">Write your main code as a sample below,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(sort_list_with_index(&#91;'Mon', 'Fri', 'Wed'], &#91;0, 2, 1]))<\/code><\/pre>\n\n\n\n<p style=\"font-size:15px\">The output of the code is,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;'Mon', 'Wed', 'Fri']<\/code><\/pre>\n\n\n\n<div class=\"wp-block-uagb-advanced-heading uagb-block-09043dce\"><h5 class=\"uagb-heading-text\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">How does the Python function work?<\/mark><\/h5><\/div>\n\n\n\n<p style=\"font-size:15px\">This function sorts a given list (<code>li1<\/code>) based on a specified order (<code>idx<\/code>) by creating a copy of the list and rearranging the elements in the copy according to the order specified by the index list.<\/p>\n\n\n\n<p style=\"font-size:15px\"><br>This Python function, <code>sort_list_with_index<\/code>, takes two parameters: <code>li1<\/code>, which is a list to be sorted, and <code>idx<\/code>, which is a list of indices that specify the order in which <code>li1<\/code> should be sorted. Here&#8217;s how it works:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>The function starts by creating a copy of the input list using the slice operator (<code>tmp = li1[:]<\/code>). This is done to avoid modifying the original list while sorting it.<\/li>\n\n\n\n<li>The function then checks if the length of the input list <code>li1<\/code> is equal to the length of the index list <code>idx<\/code>. If they are not the same length, the function displays an error message and returns <code>None<\/code>.<\/li>\n\n\n\n<li>If the length of <code>li1<\/code> is equal to the length of <code>idx<\/code>, the function proceeds to sort <code>li1<\/code> based on the order specified by the indices in <code>idx<\/code>.<\/li>\n\n\n\n<li>To sort the list, the function uses a <code>for<\/code> loop that iterates over the length of <code>idx<\/code>. In each iteration, the function sets the value of the <code>i<\/code>-th element in the copy of <code>li1<\/code> (<code>tmp[i]<\/code>) to the value of the element in <code>li1<\/code> at the index specified by <code>idx[i]<\/code>.<\/li>\n\n\n\n<li>After the loop has finished iterating, the function returns the sorted list <code>tmp<\/code>.<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Ready-to-use Python function to sort the list with an index: Write your main code as a sample below, The output of the code is, This function sorts a given list (li1) based on a specified order (idx) by creating a copy of the list and rearranging the elements in the copy according to the order [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_uag_custom_page_level_css":"","_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[12,17],"tags":[14,13,16,15,44],"class_list":["post-550","post","type-post","status-publish","format-standard","hentry","category-python","category-useful-function","tag-programming-language","tag-python","tag-python-code","tag-python-sample-code","tag-sort-list"],"aioseo_notices":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.0 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to sort the Python list with an index - tinyytopic.com<\/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:\/\/tinyytopic.com\/index.php\/2023\/02\/21\/how-to-sort-the-python-list-with-an-index\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to sort the Python list with an index - tinyytopic.com\" \/>\n<meta property=\"og:description\" content=\"Ready-to-use Python function to sort the list with an index: Write your main code as a sample below, The output of the code is, This function sorts a given list (li1) based on a specified order (idx) by creating a copy of the list and rearranging the elements in the copy according to the order [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/tinyytopic.com\/index.php\/2023\/02\/21\/how-to-sort-the-python-list-with-an-index\/\" \/>\n<meta property=\"og:site_name\" content=\"tinyytopic.com\" \/>\n<meta property=\"article:published_time\" content=\"2023-02-21T15:06:43+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-02-21T15:05:56+00:00\" \/>\n<meta name=\"author\" content=\"tinyytopic.com\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"tinyytopic.com\" \/>\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\":\"WebPage\",\"@id\":\"https:\/\/tinyytopic.com\/index.php\/2023\/02\/21\/how-to-sort-the-python-list-with-an-index\/\",\"url\":\"https:\/\/tinyytopic.com\/index.php\/2023\/02\/21\/how-to-sort-the-python-list-with-an-index\/\",\"name\":\"How to sort the Python list with an index - tinyytopic.com\",\"isPartOf\":{\"@id\":\"https:\/\/tinyytopic.com\/#website\"},\"datePublished\":\"2023-02-21T15:06:43+00:00\",\"dateModified\":\"2023-02-21T15:05:56+00:00\",\"author\":{\"@id\":\"https:\/\/tinyytopic.com\/#\/schema\/person\/56c840cea8539fb221a03c5fa2ef32eb\"},\"breadcrumb\":{\"@id\":\"https:\/\/tinyytopic.com\/index.php\/2023\/02\/21\/how-to-sort-the-python-list-with-an-index\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/tinyytopic.com\/index.php\/2023\/02\/21\/how-to-sort-the-python-list-with-an-index\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/tinyytopic.com\/index.php\/2023\/02\/21\/how-to-sort-the-python-list-with-an-index\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/tinyytopic.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to sort the Python list with an index\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/tinyytopic.com\/#website\",\"url\":\"https:\/\/tinyytopic.com\/\",\"name\":\"tinyytopic.com\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/tinyytopic.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/tinyytopic.com\/#\/schema\/person\/56c840cea8539fb221a03c5fa2ef32eb\",\"name\":\"tinyytopic.com\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/tinyytopic.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/5f153681c8ca1e6d7287d858de51f968bb687221c89cf96d763ead4393881029?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/5f153681c8ca1e6d7287d858de51f968bb687221c89cf96d763ead4393881029?s=96&d=mm&r=g\",\"caption\":\"tinyytopic.com\"},\"sameAs\":[\"http:\/\/tinyytopic.com\"],\"url\":\"https:\/\/tinyytopic.com\/index.php\/author\/mmkmuthukumar21gmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to sort the Python list with an index - tinyytopic.com","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:\/\/tinyytopic.com\/index.php\/2023\/02\/21\/how-to-sort-the-python-list-with-an-index\/","og_locale":"en_US","og_type":"article","og_title":"How to sort the Python list with an index - tinyytopic.com","og_description":"Ready-to-use Python function to sort the list with an index: Write your main code as a sample below, The output of the code is, This function sorts a given list (li1) based on a specified order (idx) by creating a copy of the list and rearranging the elements in the copy according to the order [&hellip;]","og_url":"https:\/\/tinyytopic.com\/index.php\/2023\/02\/21\/how-to-sort-the-python-list-with-an-index\/","og_site_name":"tinyytopic.com","article_published_time":"2023-02-21T15:06:43+00:00","article_modified_time":"2023-02-21T15:05:56+00:00","author":"tinyytopic.com","twitter_card":"summary_large_image","twitter_misc":{"Written by":"tinyytopic.com","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/tinyytopic.com\/index.php\/2023\/02\/21\/how-to-sort-the-python-list-with-an-index\/","url":"https:\/\/tinyytopic.com\/index.php\/2023\/02\/21\/how-to-sort-the-python-list-with-an-index\/","name":"How to sort the Python list with an index - tinyytopic.com","isPartOf":{"@id":"https:\/\/tinyytopic.com\/#website"},"datePublished":"2023-02-21T15:06:43+00:00","dateModified":"2023-02-21T15:05:56+00:00","author":{"@id":"https:\/\/tinyytopic.com\/#\/schema\/person\/56c840cea8539fb221a03c5fa2ef32eb"},"breadcrumb":{"@id":"https:\/\/tinyytopic.com\/index.php\/2023\/02\/21\/how-to-sort-the-python-list-with-an-index\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/tinyytopic.com\/index.php\/2023\/02\/21\/how-to-sort-the-python-list-with-an-index\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/tinyytopic.com\/index.php\/2023\/02\/21\/how-to-sort-the-python-list-with-an-index\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/tinyytopic.com\/"},{"@type":"ListItem","position":2,"name":"How to sort the Python list with an index"}]},{"@type":"WebSite","@id":"https:\/\/tinyytopic.com\/#website","url":"https:\/\/tinyytopic.com\/","name":"tinyytopic.com","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/tinyytopic.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/tinyytopic.com\/#\/schema\/person\/56c840cea8539fb221a03c5fa2ef32eb","name":"tinyytopic.com","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/tinyytopic.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/5f153681c8ca1e6d7287d858de51f968bb687221c89cf96d763ead4393881029?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/5f153681c8ca1e6d7287d858de51f968bb687221c89cf96d763ead4393881029?s=96&d=mm&r=g","caption":"tinyytopic.com"},"sameAs":["http:\/\/tinyytopic.com"],"url":"https:\/\/tinyytopic.com\/index.php\/author\/mmkmuthukumar21gmail-com\/"}]}},"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"tinyytopic.com","author_link":"https:\/\/tinyytopic.com\/index.php\/author\/mmkmuthukumar21gmail-com\/"},"uagb_comment_info":107,"uagb_excerpt":"Ready-to-use Python function to sort the list with an index: Write your main code as a sample below, The output of the code is, This function sorts a given list (li1) based on a specified order (idx) by creating a copy of the list and rearranging the elements in the copy according to the order&hellip;","_links":{"self":[{"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/posts\/550","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/comments?post=550"}],"version-history":[{"count":2,"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/posts\/550\/revisions"}],"predecessor-version":[{"id":552,"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/posts\/550\/revisions\/552"}],"wp:attachment":[{"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/media?parent=550"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/categories?post=550"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/tags?post=550"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}