{"id":8178,"date":"2019-12-19T23:40:50","date_gmt":"2019-12-19T18:10:50","guid":{"rendered":"https:\/\/java2blog.com\/?p=8178"},"modified":"2023-11-25T10:08:10","modified_gmt":"2023-11-25T04:38:10","slug":"python-list-to-tuple","status":"publish","type":"post","link":"https:\/\/java2blog.com\/python-list-to-tuple\/","title":{"rendered":"Python list to tuple"},"content":{"rendered":"<p>In this tutorial, we will see how to convert <a href=\"href=\" target=\"_blank\" rel=\"noopener noreferrer\">list<\/a> to tuple.<\/p>\n<div id=\"toc_container\" class=\"toc_light_blue no_bullets\"><p class=\"toc_title\">Table of Contents<\/p><ul class=\"toc_list\"><li><a href=\"#Using_tuple_function\">Using tuple function<\/a><\/li><li><a href=\"#Using_list\">Using *list,<\/a><\/li><\/ul><\/div>\n\n<hr \/>\n<h2><span id=\"Using_tuple_function\"><span style=\"color: #f89820;\">Using tuple function<\/span><\/span><\/h2>\n<p>You can use python tuple() function to convert list to tuole.It is simplest way to convert list to tuple.<\/p>\n<p>Let&#8217;s understand with the help of example.<\/p>\n<pre code=\"python\">\nlistOfFruits=['Apple','Orange','Grapes','Banana']\nprint(\"List of Fruits\",listOfFruits)\ntupleOfFruits=tuple(listOfFruits)\nprint(\"Tuple of Fruits:\",tupleOfFruits)\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"content-box-green\">List of Fruits [&#8216;Apple&#8217;, &#8216;Orange&#8217;, &#8216;Grapes&#8217;, &#8216;Banana&#8217;]\nTuple of Fruits: (&#8216;Apple&#8217;, &#8216;Orange&#8217;, &#8216;Grapes&#8217;, &#8216;Banana&#8217;)<\/div>\n<p>As you can see, tuple starts with open bracket and ends with close bracket.<\/p>\n<hr \/>\n<h2><span id=\"Using_list\"><span style=\"color: #f89820;\">Using *list,<\/span><\/span><\/h2>\n<p>There is another way to convert list to tuple in python&gt;=3.5 version.This approach is bit faster than tuple function.<\/p>\n<p>This will unpack list into tuple literal and it will be created due to , at the end.<\/p>\n<pre code=\"python\">\nlistOfFruits=['Apple','Orange','Grapes','Banana']\nprint(\"List of Fruits\",listOfFruits)\ntupleOfFruits=*listOfFruits,\nprint(\"Tuple of Fruits:\",tupleOfFruits)\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"content-box-green\">List of Fruits [&#8216;Apple&#8217;, &#8216;Orange&#8217;, &#8216;Grapes&#8217;, &#8216;Banana&#8217;]\nTuple of Fruits: (&#8216;Apple&#8217;, &#8216;Orange&#8217;, &#8216;Grapes&#8217;, &#8216;Banana&#8217;)<\/div>\n<p>&nbsp;<\/p>\n<p>That&#8217;s all about how to convert list to tuple in python.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Table of ContentsUsing tuple functionUsing *list, In this tutorial, we will see how to convert list to tuple. Using tuple function You can use python tuple() function to convert list to tuole.It is simplest way to convert list to tuple. Let&#8217;s understand with the help of example. listOfFruits=[&#8216;Apple&#8217;,&#8217;Orange&#8217;,&#8217;Grapes&#8217;,&#8217;Banana&#8217;] print(&#8220;List of Fruits&#8221;,listOfFruits) tupleOfFruits=tuple(listOfFruits) print(&#8220;Tuple of Fruits:&#8221;,tupleOfFruits) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_mi_skip_tracking":false},"categories":[145,146],"tags":[],"_links":{"self":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/8178"}],"collection":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/comments?post=8178"}],"version-history":[{"count":1,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/8178\/revisions"}],"predecessor-version":[{"id":25723,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/8178\/revisions\/25723"}],"wp:attachment":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/media?parent=8178"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/categories?post=8178"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/tags?post=8178"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}