{"id":43912,"date":"2023-03-31T08:24:49","date_gmt":"2023-03-31T08:24:49","guid":{"rendered":"https:\/\/www.askpython.com\/?p=43912"},"modified":"2023-03-31T08:24:50","modified_gmt":"2023-03-31T08:24:50","slug":"adding-tuples-to-lists","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/list\/adding-tuples-to-lists","title":{"rendered":"Adding Tuples to Lists in Python"},"content":{"rendered":"\n<p>Python is a very versatile language offering us various data structures. While programming in Python, we often work with lists and tuples. Both seem quite similar at first, but we can easily point out the differences with a closer look. According to what our program requires, we can make a fitting pick.<\/p>\n\n\n\n<p>When working with lists, we might also come across a situation where we need to add a tuple to a list. There are two ways to execute it. We\u2019ll be dealing with both ways in this article.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What are List and Tuple?<\/h2>\n\n\n\n<p>A list is a built-in data type in Python. It is a collection of ordered elements. It is used to store multiple items in a single variable, such as numbers or strings. <\/p>\n\n\n\n<p>We should keep in mind that lists are mutable, meaning you can modify the elements in a list even after it has been created. You can also add or delete elements in a list. Lists are created by enclosing a sequence of elements in square brackets and separating them with commas. Here&#8217;s an example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nmy_list= &#x5B;1, 2, a, b]\n<\/pre><\/div>\n\n\n<p>In this example, my_list is a list containing four elements: 1, 2, a, and b.<\/p>\n\n\n\n<p>A tuple is also a built-in data type in Python. It is a collection of ordered and unchangeable (immutable) elements. <\/p>\n\n\n\n<p>A tuple is very similar to a list, but the key difference is that while lists are mutable by nature, tuples aren\u2019t. Once created, we cannot modify, add or delete the elements in a tuple. Tuples are declared by enclosing a sequence of elements in round brackets separated with commas. Here&#8217;s an example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nmy_tuple = (1, 2, a, b)\n<\/pre><\/div>\n\n\n<p>Now that we&#8217;ve looked into lists and tuples, let&#8217;s understand how to add a tuple to the end of a list. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using the append() method<\/h2>\n\n\n\n<p>We can add a tuple to a list is by using the<a href=\"https:\/\/www.askpython.com\/python\/array\/append-an-array-in-python\" target=\"_blank\" rel=\"noreferrer noopener\">\u00a0append()<\/a>\u00a0method. It is a built-in method of the list object. It is used to insert an element at the end of an existing list. When using append, an element gets added to the old list instead of a new list is returned. In this case, our element is a tuple.<\/p>\n\n\n\n<p>The append() method adds a tuple to a list. It is a built-in method of the list object. It&#8217;s used to add an element to the end of an existing list. Rather than returning a new list when using append, an element is added to the existing list. Our element, in this particular case, is a tuple.<\/p>\n\n\n\n<p>Let&#8217;s look at an example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\na = &#x5B;1, 2, 3]\nb = (4, 5, 6) \na.append(b)\nprint(a)\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Output of the above code:<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\n&#x5B;1, 2, 3, (4, 5, 6)]\n<\/pre><\/div>\n\n\n<p>In the above code, we have a list \u2018a\u2019 and a tuple \u2018b\u2019. We use the &#8216;append()&#8217; to add \u2018b\u2019 to the end of \u2018a\u2019. We then print &#8216;a&#8217;.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"872\" height=\"284\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/03\/Append-Method.png\" alt=\"Append Method\" class=\"wp-image-47868\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/03\/Append-Method.png 872w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/03\/Append-Method-300x98.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/03\/Append-Method-768x250.png 768w\" sizes=\"auto, (max-width: 872px) 100vw, 872px\" \/><figcaption class=\"wp-element-caption\">Append Method<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Appending a tuple to a list using the concatenate operator<\/h2>\n\n\n\n<p>The concatenate operator can also be used to add a tuple to a list. We use the &#8216;+&#8217; operator to combine two objects, such as two strings, two lists, or a list and a tuple.<\/p>\n\n\n\n<p>The concatenate operator can be used to append a tuple to a list as shown below:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\na = &#x5B;1, 2, 3] \nb = (4, 5, 6) \nc = a + &#x5B;b]\nprint(c)\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Output:<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&#x5B;1, 2, 3, (4, 5, 6)]\n<\/pre><\/div>\n\n\n<p>In this example, we create a new list called \u2018c\u2019 by concatenating list \u2018a\u2019 with the tuple \u2018b\u2019. The resulting list contains the original elements of list \u2018a\u2019 followed by the tuple &#8216;b&#8217;.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"896\" height=\"288\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/03\/Concatenate-.png\" alt=\"Concatenate \" class=\"wp-image-47869\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/03\/Concatenate-.png 896w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/03\/Concatenate--300x96.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/03\/Concatenate--768x247.png 768w\" sizes=\"auto, (max-width: 896px) 100vw, 896px\" \/><figcaption class=\"wp-element-caption\">Concatenate Method<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Which Method to Use When Adding a Tuple?<\/h2>\n\n\n\n<p>While both the approaches demonstrated above to produce the same result, there are some differences between the two methods that we should be aware of.<\/p>\n\n\n\n<p>Firstly, the original list is modified when using the append() method, whereas, with the concatenate operator, a new list is created. With append() method, since the original list is modified, any reference to the list will also see the change. On the other hand, with the concatenate operator, since a new list is created, the original list remains unchanged.<\/p>\n\n\n\n<p>Secondly, the append() method is more efficient than the concatenate operator when you only need to add one or two elements to the list. The append() method modifies the list itself, meaning it does not need to create a new list. On the other hand, the concatenate operator needs to create a new list, which can be slower if the list is large.<\/p>\n\n\n\n<p>Thirdly, the concatenate operator is more versatile than the append() method. The + operator can concatenate two objects of any type, including lists, tuples, and even strings, whereas the append() method can only add elements to the end of a list.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Versatility of the concatenate operator:<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\na = &#x5B;1, 2, 3] \nb = (4, 5, 6) \nc = &quot;Hello&quot; \nnew_list = a+&#x5B;b]+&#x5B;c]\nprint(new_list)\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Output:<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&#x5B;1, 2, 3, (4, 5, 6), &#039;Hello&#039;]\n<\/pre><\/div>\n\n\n<p>In this example, we combine list \u2018a\u2019, tuple \u2018b\u2019, and <a href=\"https:\/\/www.askpython.com\/python\/string\/strings-in-python\" target=\"_blank\" rel=\"noreferrer noopener\">string<\/a>&nbsp;\u2018c\u2019 using the concatenate operator. The new list contains all the elements in the order they were concatenated.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"971\" height=\"303\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/03\/Versatility-Of-Operator-.png\" alt=\"Versatility Of Operator \" class=\"wp-image-47870\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/03\/Versatility-Of-Operator-.png 971w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/03\/Versatility-Of-Operator--300x94.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/03\/Versatility-Of-Operator--768x240.png 768w\" sizes=\"auto, (max-width: 971px) 100vw, 971px\" \/><figcaption class=\"wp-element-caption\">Versatility Of Operator <\/figcaption><\/figure>\n\n\n\n<p>We are now familiar with two methods for appending a tuple to a list. While the concatenate operator creates a new list and is considerably more versatile, the append() technique alters the original list and is more effective when adding only a few elements. We can choose which method to work better for our code based on our need. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">References:<\/h2>\n\n\n\n<p><a href=\"https:\/\/docs.python.org\/3\/library\/stdtypes.html#lists\" target=\"_blank\" rel=\"noreferrer noopener\">Official Documentation<\/a><br><a href=\"https:\/\/www.geeksforgeeks.org\/python-adding-tuple-to-list-and-vice-versa\/\" data-type=\"URL\" target=\"_blank\" rel=\"noopener\">Geeks For Geeks<\/a><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python is a very versatile language offering us various data structures. While programming in Python, we often work with lists and tuples. Both seem quite similar at first, but we can easily point out the differences with a closer look. According to what our program requires, we can make a fitting pick. When working with [&hellip;]<\/p>\n","protected":false},"author":62,"featured_media":44401,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-43912","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-list"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/43912","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/62"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=43912"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/43912\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/44401"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=43912"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=43912"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=43912"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}