{"id":6001,"date":"2020-05-28T03:41:47","date_gmt":"2020-05-28T03:41:47","guid":{"rendered":"https:\/\/www.askpython.com\/?p=6001"},"modified":"2023-02-16T19:57:05","modified_gmt":"2023-02-16T19:57:05","slug":"python-list-of-tuples","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/list\/python-list-of-tuples","title":{"rendered":"5 Examples of Python List of Tuples"},"content":{"rendered":"\n<p>Hey, readers! In this article, we will be focusing on <strong>Python List of Tuples<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">What is Python List and Tuple?<\/h2>\n\n\n\n<p><a href=\"https:\/\/www.askpython.com\/python\/list\/python-list\" class=\"rank-math-link\">Python List<\/a> is a data structure that maintains an ordered collection of mutable data elements.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nlist-name = &#x5B; item1, item2, ....., itemN]\n<\/pre><\/div>\n\n\n<p>The elements in the list are enclosed within <strong>square brackets []<\/strong>.<\/p>\n\n\n\n<p><a href=\"https:\/\/www.askpython.com\/python\/tuple\/python-tuple\" class=\"rank-math-link\">Python Tuple<\/a> is an immutable data structure whose elements are enclosed within <strong>parenthesis ()<\/strong>.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ntuple-name = (item1, item2, ...., itemN)\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Python List of Tuples<\/h3>\n\n\n\n<p>We can create a <strong>list of tuples<\/strong> i.e. the elements of the tuple can be enclosed in a list and thus will follow the characteristics in a similar manner as of a Python list. Since, Python Tuples utilize less amount of space, creating a list of tuples would be more useful in every aspect.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nLT_data = &#x5B;(1,2,3),(&#039;S&#039;,&#039;P&#039;,&#039;Q&#039;)]\nprint(&quot;List of Tuples:\\n&quot;,LT_data)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nList of Tuples:\n &#x5B;(1, 2, 3), (&#039;S&#039;, &#039;P&#039;, &#039;Q&#039;)]\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Python list of tuples using zip() function<\/h3>\n\n\n\n<p><a href=\"https:\/\/www.askpython.com\/python\/built-in-methods\/python-zip-function\" class=\"rank-math-link\">Python zip() function<\/a> can be used to map the lists altogether to create a list of tuples using the below command:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nlist(zip(list))\n<\/pre><\/div>\n\n\n<p>The <code>zip() function<\/code> returns an iterable of tuples based upon the values passed to it. And, further, the <code>list() function<\/code> would create a list of those tuples as an output from the zip() function.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nlst1 = &#x5B;10,20,30]\nlst2 = &#x5B;50,&quot;Python&quot;,&quot;JournalDev&quot;]\nlst_tuple = list(zip(lst1,lst2))\nprint(lst_tuple)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&#x5B;(10, 50), (20, &#039;Python&#039;), (30, &#039;JournalDev&#039;)]\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Customized grouping of elements while forming a list of tuples<\/h3>\n\n\n\n<p>While forming a list of tuples, it is possible for us to provide customized grouping of elements depending on the number of elements in the list\/tuple.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&#x5B;element for element in zip(*&#x5B;iter(list)]*number)]\n<\/pre><\/div>\n\n\n<p>List comprehension along with <code>zip() function<\/code> is used to convert the tuples to list and create a list of tuples. <code>Python iter() function<\/code> is used to iterate an element of an object at a time. The &#8216;<strong>number<\/strong>&#8216; would specify the number of elements to be clubbed into a single tuple to form a list.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nlst = &#x5B;50,&quot;Python&quot;,&quot;JournalDev&quot;,100]\nlst_tuple = &#x5B;x for x in zip(*&#x5B;iter(lst)])]\nprint(lst_tuple)\n<\/pre><\/div>\n\n\n<p>In the above example, we have formed a list of tuples with a single element inside a tuple using the <a href=\"https:\/\/www.askpython.com\/python\/python-iter-function\" class=\"rank-math-link\">iter() method<\/a>.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&#x5B;(50,), (&#039;Python&#039;,), (&#039;JournalDev&#039;,), (100,)]\n<\/pre><\/div>\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nlst = &#x5B;50,&quot;Python&quot;,&quot;JournalDev&quot;,100]\nlst_tuple = &#x5B;x for x in zip(*&#x5B;iter(lst)]*2)]\nprint(lst_tuple)\n<\/pre><\/div>\n\n\n<p>In this example, two elements are contained inside a tuple to form a list of tuple.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&#x5B;(50, &#039;Python&#039;), (&#039;JournalDev&#039;, 100)]\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Python list of tuples using map() function<\/h3>\n\n\n\n<p><a href=\"https:\/\/www.askpython.com\/python\/built-in-methods\/map-method-in-python\" class=\"rank-math-link\">Python map function<\/a> can be used to create a list of tuples. The <code>map() function<\/code> maps and applies a function to an iterable passed to the function.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nmap(function, iterable)\n<\/pre><\/div>\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nlst = &#x5B;&#x5B;50],&#x5B;&quot;Python&quot;],&#x5B;&quot;JournalDev&quot;],&#x5B;100]]\nlst_tuple =list(map(tuple, lst))\nprint(lst_tuple)\n<\/pre><\/div>\n\n\n<p>In this example, we have mapped the input list to the tuple function using map() function. After this, the list() function is used to create a list of the mapped tuple values.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&#x5B;(50,), (&#039;Python&#039;,), (&#039;JournalDev&#039;,), (100,)]\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Python list of tuples using list comprehension  and tuple() method<\/h3>\n\n\n\n<p>Python tuple() method along with List Comprehension can be used to form a list of tuples.<\/p>\n\n\n\n<p>The <code>tuple() function<\/code> helps to create tuples from the set of elements passed to it. <\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nlst = &#x5B;&#x5B;50],&#x5B;&quot;Python&quot;],&#x5B;&quot;JournalDev&quot;],&#x5B;100]]\nlst_tuple =&#x5B;tuple(ele) for ele in lst]\nprint(lst_tuple)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&#x5B;(50,), (&#039;Python&#039;,), (&#039;JournalDev&#039;,), (100,)]\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>By this, we have come to the end of the article. I hope you all have enjoyed learning this interesting concept of Python list of tuples.<\/p>\n\n\n\n<p>Feel free to comment below, in case you come across any doubt.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/stackoverflow.com\/questions\/7313157\/python-create-list-of-tuples-from-lists\/7313188\" class=\"rank-math-link\" target=\"_blank\" rel=\"noopener\">Creating a list of tuples &#8212; StackOverflow<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Hey, readers! In this article, we will be focusing on Python List of Tuples. What is Python List and Tuple? Python List is a data structure that maintains an ordered collection of mutable data elements. The elements in the list are enclosed within square brackets []. Python Tuple is an immutable data structure whose elements [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":6065,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-6001","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\/6001","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\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=6001"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/6001\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/6065"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=6001"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=6001"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=6001"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}