{"id":6118,"date":"2024-12-24T09:54:01","date_gmt":"2024-12-24T09:54:01","guid":{"rendered":"https:\/\/ubiq.co\/tech-blog\/?p=6118"},"modified":"2024-12-25T08:52:57","modified_gmt":"2024-12-25T08:52:57","slug":"how-to-flatten-list-of-lists-in-python","status":"publish","type":"post","link":"https:\/\/ubiq.co\/tech-blog\/how-to-flatten-list-of-lists-in-python\/","title":{"rendered":"How to Flatten List of Lists in Python"},"content":{"rendered":"\n<p>Sometimes Python developers need to flatten a list of lists. This means they need to convert a nested two-dimensional list into a single one-dimensional list. It simplifies data and makes it easier to process. There are several ways to do this in Python. In this article, we will learn the different ways to flatten list in Python.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-flatten-list-of-lists-in-python\/#What_is_meant_by_Flatten_List_in_Python\" >What is meant by Flatten List in Python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-flatten-list-of-lists-in-python\/#How_to_Flatten_List_of_Lists_in_Python\" >How to Flatten List of Lists in Python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-flatten-list-of-lists-in-python\/#1_Using_For_Loop\" >1. Using For Loop<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-flatten-list-of-lists-in-python\/#2_Using_itertoolschain\" >2. Using itertools.chain<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-flatten-list-of-lists-in-python\/#3_Using_List_Comprehensions\" >3. Using List Comprehensions<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-flatten-list-of-lists-in-python\/#4_Using_Numpyconcatenate\" >4. Using Numpy.concatenate<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-flatten-list-of-lists-in-python\/#5_Using_sum\" >5. Using sum()<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-flatten-list-of-lists-in-python\/#6_Using_functoolsreduce\" >6. Using functools.reduce<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-flatten-list-of-lists-in-python\/#7_Using_operator\" >7. Using operator<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-flatten-list-of-lists-in-python\/#Conclusion\" >Conclusion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-flatten-list-of-lists-in-python\/#FAQ\" >FAQ<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"What_is_meant_by_Flatten_List_in_Python\"><\/span>What is meant by Flatten List in Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Lists are versatile data structures used by many Python developers. A list can contain numbers, texts and even other lists as individual items. When you have a list where each item itself is a list, then it is called nested list or list of lists. It can be difficult to work with list of lists. Often Python developers need to convert this into a single list by copying the individual items of original list to a new list. This is called flattening of list in Python. Let us say you have the following list of lists.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">data = [[1, 2],[3, 4],[5, 6]]<\/pre>\n\n\n\n<p>Let us say you want to flatten the above list. In this case, you will obtain the following result.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">new_data = [1, 2, 3, 4, 5, 6]<\/pre>\n\n\n\n<p>As you can see above, the result is a single list with all items from original list present.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How_to_Flatten_List_of_Lists_in_Python\"><\/span>How to Flatten List of Lists in Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Here are the different ways to flatten list of lists in Python.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"1_Using_For_Loop\"><\/span>1. Using For Loop<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>One of the most common methods is to simply loop through the list of lists and in each iteration, we append the inner list&#8217;s item to the resultant flat list. Here is a code to demonstrate it.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt;&gt;&gt; new_data = []<br>&gt;&gt;&gt; for i in data:<br>        for j in i:<br>                new_data.append(j)<br><br>&gt;&gt;&gt; new_data<br>[1, 2, 3, 4, 5, 6]<\/pre>\n\n\n\n<p>Every list supports append() function that allows you to append a new item to the list. We use it to copy elements from original list to new list.<\/p>\n\n\n\n<p>In the above case, we need to run two loops, one within the other. Alternatively, you can iterate over the original list and in each iteration, use extend() function to easily append all items of each row to the new list.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt;&gt;&gt; new_data = []<br>&gt;&gt;&gt; for i in data:<br>        new_data.extend(i)<br><br>&gt;&gt;&gt; new_data<br>[1, 2, 3, 4, 5, 6]<\/pre>\n\n\n\n<p>In the above case, you need only one loop. So it is much faster than using append() function earlier.<\/p>\n\n\n\n<p>You can also loop through the list of lists and in each iteration, use concatenation operator (+), to append the list item to new flattened list.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt;&gt;&gt; new_data=[]<br>&gt;&gt;&gt; for i in data:<br>        new_data+=i<br><br>&gt;&gt;&gt; new_data<br>[1, 2, 3, 4, 5, 6]<\/pre>\n\n\n\n<p>This method is quite straightforward, easy to understand and can be customized as per your requirement. For example, you can choose to exclude certain items from the final list, by adding if&#8230;else condition inside for loops.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"2_Using_itertoolschain\"><\/span>2. Using itertools.chain<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Itertools is a popular &amp; efficient Python module that provides many useful functions that use iterators to quickly loop through data. In other words, it provides functions to loop through large data with minimal memory at fast pace.<\/p>\n\n\n\n<p>You can use itertools.chain() or itertools.chain.from_iterable() function to flatten a list. Let us look at both of them.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt;&gt;&gt; import itertools<br>&gt;&gt;&gt; new_data=[]<br>&gt;&gt;&gt; new_data=list(itertools.chain(*data))<br>&gt;&gt;&gt; new_data<br>[1, 2, 3, 4, 5, 6]<\/pre>\n\n\n\n<p>In the above code, we simply provide the original list of lists, preceded by unpacking operator (*) in itertools.chain(). It will do the rest and flatten the list.<\/p>\n\n\n\n<p>If you use itertools.chain.from_iterable(), then you do not even need to provide the unpacking operator.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt;&gt;&gt; import itertools<br>&gt;&gt;&gt; new_data=[]<br>&gt;&gt;&gt; new_data=list(itertools.chain.from_iterable(data))<br>&gt;&gt;&gt; new_data<br>[1, 2, 3, 4, 5, 6]<\/pre>\n\n\n\n<p>You can use itertools.chain() or itertools.chain.from_iterable() for small as well as large list of lists. Since it uses iterators, it does not load the data completely in memory. Instead, it just uses the iterators to the list, which hardly takes up any space. Also, it is significantly faster than other methods and highly scalable.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"3_Using_List_Comprehensions\"><\/span>3. Using List Comprehensions<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>List comprehensions are one line expressions or statements that allow you to easily perform a lot of operations on a list. They are basically similar to loops but much faster. They can be used to flatten a list of lists. Here is their basic syntax.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[expression(item) for item in iterable]<\/pre>\n\n\n\n<p>Here is an example to flatten our list.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt;&gt;&gt; new_data=[]<br>&gt;&gt;&gt; new_data=[item for row in data for item in row]<br>&gt;&gt;&gt; new_data<br>[1, 2, 3, 4, 5, 6]<\/pre>\n\n\n\n<p>In the above list comprehension, there are 2 for loops. The first one iterates over each list(row) in the original list. The second one iterates over each item in the row.<\/p>\n\n\n\n<p>Although it looks a little complicated, it is much faster than using sum() or reduce() functions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"4_Using_Numpyconcatenate\"><\/span>4. Using Numpy.concatenate<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Numpy is a popular Python library for data analysis. It provides many functions to work with numbers. If your list of lists consists of only numbers, then you can use numpy.concatenate() function to flatten such as list.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt;&gt;&gt; import numpy as np<br><br>&gt;&gt;&gt; new_data = np.concatenate(data).tolist()<br>&gt;&gt;&gt; new_data<br>[1, 2, 3, 4, 5, 6]<\/pre>\n\n\n\n<p>In the above code, we also need to call tolist() function to convert the output of numpy.concatenate() to a list.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"5_Using_sum\"><\/span>5. Using sum()<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>You can also use sum() function to flatten a list. It will begin with an empty list and add the individual lists of list of lists, into it, one by one. So it is important to pass an empty list as the second argument of sum() function.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt;&gt;&gt; new_data=[]<br>&gt;&gt;&gt; new_data=sum(data,[])<br>&gt;&gt;&gt; new_data<br>[1, 2, 3, 4, 5, 6]<\/pre>\n\n\n\n<p>It is not as fast as other methods listed above but it is super simple and easy to understand. It works well for small list of lists. You can also pass a non-empty list as second argument, if you want to not only flatten your list but concatenate it with another list. <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt;&gt;&gt; new_data=sum(data,[10])<br><br>&gt;&gt;&gt; new_data<br>[10, 1, 2, 3, 4, 5, 6]<\/pre>\n\n\n\n<p>So it is a very convenient shortcut to easily flatten and concatenate lists or list or lists. But please note, if you want to flatten &amp; concatenate two list of lists, you need to use two sum() functions, one for each list of lists, as shown below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt;&gt;&gt; data = [[1,2],[3,4],[5,6]]<br>&gt;&gt;&gt; data1 = [[10,11],[12,13],[14,15]]<br>&gt;&gt;&gt; new_data = sum(data,sum(data1,[]))<br>&gt;&gt;&gt; new_data <br>[10, 11, 12, 13, 14, 15, 1, 2, 3, 4, 5, 6]<\/pre>\n\n\n\n<p>In the above code, we call sum() function twice. First, to flatten second list, and then use it to flatten the first list.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"6_Using_functoolsreduce\"><\/span>6. Using functools.reduce<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Python also provides functools library for functional programming. Among them, reduce() is a powerful function that offers many uses. It takes a couple of arguments, performs desired tasks, computes and stores partial result. Then it iterates to the next item, and uses it along with previous result to perform another computation. It once again stores the result. It continues this process till end of items. It is very useful for flattening a list in Python. <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt;&gt;&gt; data=[[1,2],[3,4],[5,6]]<br>&gt;&gt;&gt; new_data = reduce(lambda x,y:x+y,data)<br>&gt;&gt;&gt; new_data<br>[1, 2, 3, 4, 5, 6]<\/pre>\n\n\n\n<p>In the above code, we use lambda function in reduce(). Lambda functions are small, anonymous functions that contain just a single statement or expression. It takes couple of arguments x, y and concatenates them using + operator. The first argument is a lambda function. The second argument concatenates 2 items and stores the result in y. The third argument is the data list of lists. <\/p>\n\n\n\n<p>So basically, the lambda function starts with an empty list. It takes the first 2 items of the list of lists, and concatenates them. In other words, it joins the first 2 rows to create a single list. Then it concatenates this list with the next item of the list, that is, the third row of the list of lists to create the final result.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"7_Using_operator\"><\/span>7. Using operator<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In the above solution, we have manually defined the lambda function to concatenate list of lists. Python also provides <em>operator<\/em> library that contains many handy functions for efficiently performing certain operations in Python. You can use them with reduce() function to easily concatenate list of lists. Here is an example that uses add(), concat() and iconcat() operator functions which can be directly invoked on any data item.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&gt;&gt;&gt; from operator import add, concat, iconcat<br>&gt;&gt;&gt; from functools import reduce<br><br>&gt;&gt;&gt; new_data=reduce(add,data)<br>&gt;&gt;&gt; new_data<br>[1, 2, 3, 4, 5, 6]<br><br>&gt;&gt;&gt; new_data=reduce(concat,data)<br>&gt;&gt;&gt; new_data<br>[1, 2, 3, 4, 5, 6]<br><br>&gt;&gt;&gt; new_data=reduce(iconcat,data)<br>&gt;&gt;&gt; new_data<br>[1, 2, 3, 4, 5, 6]<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In this article, we have learnt many different ways to flatten list of lists in Python. You can use any of these methods depending on your requirement. Among them, using itertools and list comprehensions provide the fastest ways to flatten a list of lists. Numpy.concatenate() function works with only list of lists containing numbers. All other methods are suitable for all data types.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"FAQ\"><\/span>FAQ<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p><strong>1. Which is the fastest way to flatten list of lists?<\/strong><\/p>\n\n\n\n<p>Using itertools or list comprehensions provide the fastest ways to flatten list of lists.<\/p>\n\n\n\n<p><strong>2. Do these solutions work for all data types?<\/strong><\/p>\n\n\n\n<p>Using numpy.concatenate() works for only numeric data types. All other solutions work for all data types.<\/p>\n\n\n\n<p>Also read:<\/p>\n\n\n\n<p><a href=\"https:\/\/ubiq.co\/tech-blog\/how-to-access-index-value-in-for-loop-in-python\/\">How to Access Index Value in For Loop in Python<\/a><br><a href=\"https:\/\/ubiq.co\/tech-blog\/how-to-create-directory-with-missing-parent-directory\/\">How to Create Python Directory With Missing Parent<\/a><br><a href=\"https:\/\/ubiq.co\/tech-blog\/does-python-have-ternary-operator\/\">Does Python Have Ternary Operator?<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes Python developers need to flatten list of lists in Python. Here are different ways to do it.<\/p>\n","protected":false},"author":1,"featured_media":6151,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[301],"tags":[361],"class_list":["post-6118","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-flatten-list"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to Flatten List of Lists in Python - Ubiq BI<\/title>\n<meta name=\"description\" content=\"Sometimes Python developers need to flatten list of lists in Python. Here are different ways to do it.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/ubiq.co\/tech-blog\/how-to-flatten-list-of-lists-in-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Flatten List of Lists in Python - Ubiq BI\" \/>\n<meta property=\"og:description\" content=\"Sometimes Python developers need to flatten list of lists in Python. Here are different ways to do it.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/ubiq.co\/tech-blog\/how-to-flatten-list-of-lists-in-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Ubiq BI\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/ubiqbi\" \/>\n<meta property=\"article:published_time\" content=\"2024-12-24T09:54:01+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-12-25T08:52:57+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/12\/flatten-list-of-lists-python.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"300\" \/>\n\t<meta property=\"og:image:height\" content=\"200\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Sreeram Sreenivasan\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@UbiqBI\" \/>\n<meta name=\"twitter:site\" content=\"@UbiqBI\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Sreeram Sreenivasan\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-flatten-list-of-lists-in-python\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-flatten-list-of-lists-in-python\\\/\"},\"author\":{\"name\":\"Sreeram Sreenivasan\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#\\\/schema\\\/person\\\/db98d49a766a3a111d8510935ab90abc\"},\"headline\":\"How to Flatten List of Lists in Python\",\"datePublished\":\"2024-12-24T09:54:01+00:00\",\"dateModified\":\"2024-12-25T08:52:57+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-flatten-list-of-lists-in-python\\\/\"},\"wordCount\":1332,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-flatten-list-of-lists-in-python\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2024\\\/12\\\/flatten-list-of-lists-python.jpg?fit=300%2C200&ssl=1\",\"keywords\":[\"flatten list\"],\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-flatten-list-of-lists-in-python\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-flatten-list-of-lists-in-python\\\/\",\"url\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-flatten-list-of-lists-in-python\\\/\",\"name\":\"How to Flatten List of Lists in Python - Ubiq BI\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-flatten-list-of-lists-in-python\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-flatten-list-of-lists-in-python\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2024\\\/12\\\/flatten-list-of-lists-python.jpg?fit=300%2C200&ssl=1\",\"datePublished\":\"2024-12-24T09:54:01+00:00\",\"dateModified\":\"2024-12-25T08:52:57+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#\\\/schema\\\/person\\\/db98d49a766a3a111d8510935ab90abc\"},\"description\":\"Sometimes Python developers need to flatten list of lists in Python. Here are different ways to do it.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-flatten-list-of-lists-in-python\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-flatten-list-of-lists-in-python\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-flatten-list-of-lists-in-python\\\/#primaryimage\",\"url\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2024\\\/12\\\/flatten-list-of-lists-python.jpg?fit=300%2C200&ssl=1\",\"contentUrl\":\"https:\\\/\\\/i0.wp.com\\\/ubiq.co\\\/tech-blog\\\/wp-content\\\/uploads\\\/2024\\\/12\\\/flatten-list-of-lists-python.jpg?fit=300%2C200&ssl=1\",\"width\":300,\"height\":200,\"caption\":\"flatten list of lists in python\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/how-to-flatten-list-of-lists-in-python\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Flatten List of Lists in Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#website\",\"url\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/\",\"name\":\"Ubiq BI\",\"description\":\"Build dashboards &amp; reports in minutes\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/#\\\/schema\\\/person\\\/db98d49a766a3a111d8510935ab90abc\",\"name\":\"Sreeram Sreenivasan\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g\",\"caption\":\"Sreeram Sreenivasan\"},\"description\":\"Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI &amp; software development.\",\"sameAs\":[\"https:\\\/\\\/www.linkedin.com\\\/in\\\/sreeram-sreenivasan\\\/\"],\"url\":\"https:\\\/\\\/ubiq.co\\\/tech-blog\\\/author\\\/wordpress\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Flatten List of Lists in Python - Ubiq BI","description":"Sometimes Python developers need to flatten list of lists in Python. Here are different ways to do it.","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:\/\/ubiq.co\/tech-blog\/how-to-flatten-list-of-lists-in-python\/","og_locale":"en_US","og_type":"article","og_title":"How to Flatten List of Lists in Python - Ubiq BI","og_description":"Sometimes Python developers need to flatten list of lists in Python. Here are different ways to do it.","og_url":"https:\/\/ubiq.co\/tech-blog\/how-to-flatten-list-of-lists-in-python\/","og_site_name":"Ubiq BI","article_publisher":"https:\/\/www.facebook.com\/ubiqbi","article_published_time":"2024-12-24T09:54:01+00:00","article_modified_time":"2024-12-25T08:52:57+00:00","og_image":[{"width":300,"height":200,"url":"https:\/\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/12\/flatten-list-of-lists-python.jpg","type":"image\/jpeg"}],"author":"Sreeram Sreenivasan","twitter_card":"summary_large_image","twitter_creator":"@UbiqBI","twitter_site":"@UbiqBI","twitter_misc":{"Written by":"Sreeram Sreenivasan","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/ubiq.co\/tech-blog\/how-to-flatten-list-of-lists-in-python\/#article","isPartOf":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-flatten-list-of-lists-in-python\/"},"author":{"name":"Sreeram Sreenivasan","@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc"},"headline":"How to Flatten List of Lists in Python","datePublished":"2024-12-24T09:54:01+00:00","dateModified":"2024-12-25T08:52:57+00:00","mainEntityOfPage":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-flatten-list-of-lists-in-python\/"},"wordCount":1332,"commentCount":0,"image":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-flatten-list-of-lists-in-python\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/12\/flatten-list-of-lists-python.jpg?fit=300%2C200&ssl=1","keywords":["flatten list"],"articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/ubiq.co\/tech-blog\/how-to-flatten-list-of-lists-in-python\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/ubiq.co\/tech-blog\/how-to-flatten-list-of-lists-in-python\/","url":"https:\/\/ubiq.co\/tech-blog\/how-to-flatten-list-of-lists-in-python\/","name":"How to Flatten List of Lists in Python - Ubiq BI","isPartOf":{"@id":"https:\/\/ubiq.co\/tech-blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-flatten-list-of-lists-in-python\/#primaryimage"},"image":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-flatten-list-of-lists-in-python\/#primaryimage"},"thumbnailUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/12\/flatten-list-of-lists-python.jpg?fit=300%2C200&ssl=1","datePublished":"2024-12-24T09:54:01+00:00","dateModified":"2024-12-25T08:52:57+00:00","author":{"@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc"},"description":"Sometimes Python developers need to flatten list of lists in Python. Here are different ways to do it.","breadcrumb":{"@id":"https:\/\/ubiq.co\/tech-blog\/how-to-flatten-list-of-lists-in-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/ubiq.co\/tech-blog\/how-to-flatten-list-of-lists-in-python\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ubiq.co\/tech-blog\/how-to-flatten-list-of-lists-in-python\/#primaryimage","url":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/12\/flatten-list-of-lists-python.jpg?fit=300%2C200&ssl=1","contentUrl":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/12\/flatten-list-of-lists-python.jpg?fit=300%2C200&ssl=1","width":300,"height":200,"caption":"flatten list of lists in python"},{"@type":"BreadcrumbList","@id":"https:\/\/ubiq.co\/tech-blog\/how-to-flatten-list-of-lists-in-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/ubiq.co\/tech-blog\/"},{"@type":"ListItem","position":2,"name":"How to Flatten List of Lists in Python"}]},{"@type":"WebSite","@id":"https:\/\/ubiq.co\/tech-blog\/#website","url":"https:\/\/ubiq.co\/tech-blog\/","name":"Ubiq BI","description":"Build dashboards &amp; reports in minutes","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/ubiq.co\/tech-blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/ubiq.co\/tech-blog\/#\/schema\/person\/db98d49a766a3a111d8510935ab90abc","name":"Sreeram Sreenivasan","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4b3127ed2d4bb8efb3fa0bbb52cf2efd4d0156c97fc05a503537c883e8279947?s=96&d=mm&r=g","caption":"Sreeram Sreenivasan"},"description":"Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI &amp; software development.","sameAs":["https:\/\/www.linkedin.com\/in\/sreeram-sreenivasan\/"],"url":"https:\/\/ubiq.co\/tech-blog\/author\/wordpress\/"}]}},"jetpack_featured_media_url":"https:\/\/i0.wp.com\/ubiq.co\/tech-blog\/wp-content\/uploads\/2024\/12\/flatten-list-of-lists-python.jpg?fit=300%2C200&ssl=1","jetpack_shortlink":"https:\/\/wp.me\/pbGGTT-1AG","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/6118","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/comments?post=6118"}],"version-history":[{"count":36,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/6118\/revisions"}],"predecessor-version":[{"id":6199,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/posts\/6118\/revisions\/6199"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/media\/6151"}],"wp:attachment":[{"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/media?parent=6118"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/categories?post=6118"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ubiq.co\/tech-blog\/wp-json\/wp\/v2\/tags?post=6118"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}