{"id":8127,"date":"2024-04-11T12:03:12","date_gmt":"2024-04-11T12:03:12","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=8127"},"modified":"2024-04-11T12:03:12","modified_gmt":"2024-04-11T12:03:12","slug":"merge-two-lists-in-python","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/merge-two-lists-in-python\/","title":{"rendered":"Merge Two Lists in Python"},"content":{"rendered":"\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\" id=\"rank-math-toc\"><p>Table of Contents<\/p><nav><ul><li ><a href=\"#what-is-python-join-two-lists\">What is Python Join Two Lists?<\/a><\/li><li ><a href=\"#how-to-merge-two-lists-in-python-using-method\">How to Merge two lists in Python using Method<\/a><\/li><li ><a href=\"#how-to-merge-two-lists-using-the-extend\">How to Merge two lists using the extend()<\/a><\/li><li ><a href=\"#how-to-join-two-lists-using-operator-in-python\">How to  Join Two Lists Using * Operator in Python?<\/a><\/li><li ><a href=\"#how-to-join-two-lists-using-the-itertools-chain-in-python\">How to join two lists using the itertools. chain() in Python?<\/a><\/li><li ><a href=\"#how-to-concatenate-two-lists-using-the-reduce-function-in-python\">How to Concatenate two lists using the reduce ()  function in  Python?<\/a><\/li><li ><a href=\"#conclusion\">Conclusion<\/a><\/li><li ><a href=\"#merge-two-lists-in-python-fa-qs\">Merge Two Lists in Python- FAQs<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<p>In  <a href=\"https:\/\/www.skillvertex.com\/blog\/python-access-list-items\/\" data-type=\"post\" data-id=\"8035\">Python<\/a>, there are many ways to join, or concatenate, two or more lists in Python. Check out the article below to learn more about Merge Two <a href=\"https:\/\/www.skillvertex.com\/blog\/python-access-list-items\/\" data-type=\"post\" data-id=\"8035\">Lists <\/a>in Python.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-python-join-two-lists\">What is Python Join Two Lists?<\/h2>\n\n\n\n<p><a href=\"https:\/\/www.skillvertex.com\/blog\/c-program-to-merge-contents-of-two-files-into-a-third-file\/\" data-type=\"post\" data-id=\"3634\">Merging <\/a>a <a href=\"https:\/\/www.skillvertex.com\/blog\/python-list-sort-method\/\" data-type=\"post\" data-id=\"8100\">list <\/a>refers to adding or concatenating one list with another. Also, it refers to adding two lists. The methods that are used to join the <a href=\"https:\/\/www.skillvertex.com\/blog\/python-add-list-items\/\" data-type=\"post\" data-id=\"8058\">Python <\/a>list are given below:<\/p>\n\n\n\n<p>a. Using the Naive Method<\/p>\n\n\n\n<p>b.Using the<a href=\"https:\/\/www.skillvertex.com\/blog\/assignment-operators-in-python\/\" data-type=\"post\" data-id=\"6970\"> + operator<\/a><\/p>\n\n\n\n<p>c.Using the list comprehension<\/p>\n\n\n\n<p>d.Using the <a href=\"https:\/\/www.skillvertex.com\/blog\/python-bitwise-operator\/\" data-type=\"post\" data-id=\"6985\">extend () method<\/a><\/p>\n\n\n\n<p>e.Using the <a href=\"https:\/\/www.skillvertex.com\/blog\/and-operators-in-c\/\" data-type=\"post\" data-id=\"3294\">* operator<\/a><\/p>\n\n\n\n<p>f.Using the itertools.chains<\/p>\n\n\n\n<p>g.Merge two List using reduce <\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"how-to-merge-two-lists-in-python-using-method\">How to Merge two lists in Python using Method<\/h2>\n\n\n\n<p>The list comprehension will help to accomplish the task of the <a href=\"https:\/\/www.skillvertex.com\/blog\/python-add-list-items\/\" data-type=\"post\" data-id=\"8058\">list <\/a>concatenation. Whereas, the new <a href=\"https:\/\/www.skillvertex.com\/blog\/python-copy-list\/\" data-type=\"post\" data-id=\"8109\">list <\/a>will be created.<\/p>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Method 1: Using extend() method\ndef merge_lists_method1(list1, list2):\n    merged_list = list1.copy()  # Make a copy of list1 to preserve original data\n    merged_list.extend(list2)  # Extend the list with elements from list2\n    return merged_list\n\n# Method 2: Using + operator\ndef merge_lists_method2(list1, list2):\n    merged_list = list1 + list2  # Concatenate the two lists using the + operator\n    return merged_list\n\n# Example lists\nlist1 = &#91;1, 2, 3]\nlist2 = &#91;4, 5, 6]\n\n# Output of Method 1\nmerged_list_method1 = merge_lists_method1(list1, list2)\nprint(\"Merged list using extend() method:\", merged_list_method1)\n\n# Output of Method 2\nmerged_list_method2 = merge_lists_method2(list1, list2)\nprint(\"Merged list using + operator:\", merged_list_method2)\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Merged list using extend() method: &#91;1, 2, 3, 4, 5, 6]\nMerged list using + operator: &#91;1, 2, 3, 4, 5, 6]\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"how-to-merge-two-lists-using-the-extend\">How to Merge two lists using the extend()<\/h2>\n\n\n\n<p>The extend() is a <a href=\"https:\/\/www.skillvertex.com\/blog\/c-fopen-function-with-examples\/\" data-type=\"post\" data-id=\"3556\">function <\/a>that allows you to extend by <a href=\"https:\/\/www.skillvertex.com\/blog\/python-loop-lists\/\" data-type=\"post\" data-id=\"8077\">list <\/a>in Python and then perform the task. So, this function will do the in-place extension of the first list.<\/p>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def merge_lists(list1, list2):\n    merged_list = list1.copy()  # Make a copy of list1 to preserve original data\n    merged_list.extend(list2)  # Extend the list with elements from list2\n    return merged_list\n\n# Example lists\nlist1 = &#91;1, 2, 3]\nlist2 = &#91;4, 5, 6]\n\n# Merge the lists using extend() method\nmerged_list = merge_lists(list1, list2)\n\n# Output the merged list\nprint(\"Merged list using extend() method:\", merged_list)\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Merged list using extend() method: &#91;1, 2, 3, 4, 5, 6]\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"how-to-join-two-lists-using-operator-in-python\">How to  Join Two Lists Using * Operator in Python?<\/h2>\n\n\n\n<p>This method will be performed with the help of the * <a href=\"https:\/\/www.skillvertex.com\/blog\/increment-and-decrement-operators-in-c\/\" data-type=\"post\" data-id=\"2258\">operator<\/a>. It is the new addition to the list concatenation and will work only in <a href=\"https:\/\/www.skillvertex.com\/blog\/python-history-and-versions\/\" data-type=\"post\" data-id=\"6864\">Python 3.6+.<\/a> So,  any no of the list will be concatenated and returned to the new list using the <a href=\"https:\/\/www.skillvertex.com\/blog\/operators-in-c-set-2-relational-and-logical-operators\/\" data-type=\"post\" data-id=\"2202\">operator<\/a>.<\/p>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def join_lists(list1, list2):\n    joined_list = &#91;*list1, *list2]  # Concatenate the two lists using the * operator\n    return joined_list\n\n# Example lists\nlist1 = &#91;1, 2, 3]\nlist2 = &#91;4, 5, 6]\n\n# Join the lists using * operator\njoined_list = join_lists(list1, list2)\n\n# Output the joined list\nprint(\"Joined list using * operator:\", joined_list)\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Joined list using * operator: &#91;1, 2, 3, 4, 5, 6]\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"how-to-join-two-lists-using-the-itertools-chain-in-python\">How to join two lists using the itertools. chain() in Python?<\/h2>\n\n\n\n<p>itertools.chain() is a <a href=\"https:\/\/www.skillvertex.com\/blog\/c-string-functions\/\" data-type=\"post\" data-id=\"2675\">function <\/a>in <a href=\"https:\/\/www.skillvertex.com\/blog\/python-access-list-items\/\" data-type=\"post\" data-id=\"8035\">Python <\/a>that combines multiple <a href=\"https:\/\/www.skillvertex.com\/blog\/python-loop-lists\/\" data-type=\"post\" data-id=\"8077\">lists <\/a>into a single iterable (something you can iterate over, like a list). Hence, it doesn&#8217;t create a new list by combining them, instead, it just gives you a way to access all the <a href=\"https:\/\/www.skillvertex.com\/blog\/which-one-is-not-an-element-of-iot\/\" data-type=\"post\" data-id=\"3089\">elements <\/a>from all the lists one after the other without storing them in <a href=\"https:\/\/www.skillvertex.com\/blog\/what-is-a-memory-leak-how-can-we-avoid-it\/\" data-type=\"post\" data-id=\"3174\">memory<\/a>.<\/p>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import itertools\n\n# Define the lists\nlist1 = &#91;1, 2, 3]\nlist2 = &#91;4, 5, 6]\n\n# Use itertools.chain() to join the lists\njoined_list = itertools.chain(list1, list2)\n\n# Output the joined list\nprint(\"Joined list using itertools.chain():\", list(joined_list))\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Joined list using itertools.chain(): &#91;1, 2, 3, 4, 5, 6]\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"how-to-concatenate-two-lists-using-the-reduce-function-in-python\">How to Concatenate two lists using the reduce ()  function in  Python?<\/h2>\n\n\n\n<p>First, we import the reduce function from the functools <a href=\"https:\/\/www.skillvertex.com\/blog\/c-library-math-h-functions\/\" data-type=\"post\" data-id=\"2596\">library<\/a>, which helps us perform <a href=\"https:\/\/www.skillvertex.com\/blog\/which-part-interprets-program-instructions-and-initiate-control-operations\/\" data-type=\"post\" data-id=\"1998\">operations <\/a>on lists. Then, we create two <a href=\"https:\/\/www.skillvertex.com\/blog\/python-list-sort-method\/\" data-type=\"post\" data-id=\"8100\">lists<\/a>, list1, and list2, each containing some <a href=\"https:\/\/www.skillvertex.com\/blog\/which-one-is-not-an-element-of-iot\/\" data-type=\"post\" data-id=\"3089\">elements<\/a>. Next, we combine these lists into a nested list called nested_list. Using the reduce() <a href=\"https:\/\/www.skillvertex.com\/blog\/static-functions-in-c\/\" data-type=\"post\" data-id=\"3038\">function<\/a>, we merge the <a href=\"https:\/\/www.skillvertex.com\/blog\/python-loop-lists\/\" data-type=\"post\" data-id=\"8077\">lists <\/a>within nested_list. <\/p>\n\n\n\n<p>Within the reduce() function, we use a lambda <a href=\"https:\/\/www.skillvertex.com\/blog\/c-fopen-function-with-examples\/\" data-type=\"post\" data-id=\"3556\">function<\/a>, which works similarly to the mini-function used for a single purpose, to concatenate each list in the <a href=\"https:\/\/www.skillvertex.com\/blog\/python-nested-loops\/\" data-type=\"post\" data-id=\"7259\">nested list<\/a>. Finally, the result of the concatenation is stored in the variable result, which holds the joined list. This joined list is then printed as the final output.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"conclusion\">Conclusion<\/h2>\n\n\n\n<p>In Python, merging two lists can be achieved using different methods, such as the extend() method, the + operator, and itertools. chain(), or even the reduce() function from the functools library. Each method offers its way of combining lists, catering to different needs and preferences. <\/p>\n\n\n\n<p>Whether you&#8217;re simply concatenating lists, chaining their elements together, or reducing them into a single list, Python provides versatile solutions to merge lists efficiently. By understanding these methods, you can choose the one that best fits your specific requirements when working with lists in Python.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"merge-two-lists-in-python-fa-qs\">Merge Two Lists in Python- FAQs<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1710411469363\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1.How do you merge two lists in Python?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. This symbol will allow you to combine all the items from one list with those from another.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1710411476803\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2.How do I combine two unique lists in Python?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. To merge the two unique lists,  we use the concatenation operator (+) to join them together.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1710411484898\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3. How do you join a list together in Python?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. Use the string = &#8216; &#8216;.\u00a0join(list)\u00a0to join the list to the string in Python.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>In Python, there are many ways to join, or concatenate, two or more lists in Python. Check out the article below to learn more about Merge Two Lists in Python. What is Python Join Two Lists? Merging a list refers to adding or concatenating one list with another. Also, it refers to adding two lists. &#8230; <a title=\"Merge Two Lists in Python\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/merge-two-lists-in-python\/\" aria-label=\"More on Merge Two Lists in Python\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":8135,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[864],"tags":[975,57,964,866],"class_list":["post-8127","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-tutorial","tag-merge-two-lists-in-python","tag-python","tag-python-list","tag-python-tutorial","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"_links":{"self":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/8127","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/comments?post=8127"}],"version-history":[{"count":12,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/8127\/revisions"}],"predecessor-version":[{"id":8913,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/8127\/revisions\/8913"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/8135"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=8127"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=8127"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=8127"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}