{"id":8810,"date":"2023-10-11T11:10:43","date_gmt":"2023-10-11T11:10:43","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=8810"},"modified":"2023-10-11T11:10:43","modified_gmt":"2023-10-11T11:10:43","slug":"make-a-flat-list-of-lists-in-python","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/python\/make-a-flat-list-of-lists-in-python","title":{"rendered":"How to Make a Flat List Out of a List of Lists?"},"content":{"rendered":"<p>Using list comprehension It\u2019s very easy to convert a two dimensional list into a flat (single dimensional) list.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">flat_list = [num for childlist in lists for num in childlist]\r\n<\/pre>\n<p><strong>Limitations:<\/strong><\/p>\n<ul>\n<li aria-level=\"1\">This is the optimal approach if your parent list has components that can be iterated, such as lists or strings, but it won&#8217;t work for non-literals like integers and floats.<\/li>\n<li aria-level=\"1\">Another situation where this technique cannot be applied is a list with more than two dimensions.<\/li>\n<\/ul>\n<p><strong>Solution:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">L = [\r\n[1,2,3],\r\n[\r\n \t[4,5,6],\r\n \t[7,8,9],\r\n \t[\r\n \t\t[10,11,12],\r\n \t \t\t[13,14,15]\r\n \t]\r\n],\r\n16,17,18\r\n]\r\n\r\n\r\ndef flat_my_list(L: list):\r\nresult = []\r\nfor element in in L:\r\n      try:\r\n      \titer(element)\r\n           \tresult.extend(flat_my_list(element))\r\n \texcept:\r\n \t\tresult.append(element)\r\nreturn result\r\n\r\n\r\nflat_my_list(L)<\/pre>\n<p>All of those criteria will be fulfilled by the function flat_my_list.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Using list comprehension It\u2019s very easy to convert a two dimensional list into a flat (single dimensional) list. flat_list = [num for childlist in lists for num in childlist] Limitations: This is the optimal approach if your parent list has components that can be iterated, such as lists or strings, but it won&#8217;t work for [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":8811,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[16],"tags":[],"class_list":["post-8810","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/8810"}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/comments?post=8810"}],"version-history":[{"count":1,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/8810\/revisions"}],"predecessor-version":[{"id":8812,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/8810\/revisions\/8812"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/8811"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=8810"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=8810"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=8810"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}