{"id":127486,"date":"2022-08-23T16:59:04","date_gmt":"2022-08-23T16:59:04","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=606497"},"modified":"2022-08-23T16:59:04","modified_gmt":"2022-08-23T16:59:04","slug":"how-to-create-a-python-tuple-of-size-n","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2022\/08\/23\/how-to-create-a-python-tuple-of-size-n\/","title":{"rendered":"How to Create a Python Tuple of Size n?"},"content":{"rendered":"\n<div class=\"kk-star-ratings kksr-auto kksr-align-left kksr-valign-top\" data-payload=\"{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;606497&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;1&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;5&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;5\\\/5 - (1 vote)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;width&quot;:&quot;142.5&quot;,&quot;_legend&quot;:&quot;{score}\\\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}\">\n<div class=\"kksr-stars\">\n<div class=\"kksr-stars-inactive\">\n<div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div class=\"kksr-stars-active\" style=\"width: 142.5px;\">\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/div>\n<div class=\"kksr-legend\" style=\"font-size: 19.2px;\"> 5\/5 &#8211; (1 vote) <\/div>\n<\/div>\n<p class=\"has-global-color-8-background-color has-background\">Use the tuple concatenation operation <code>*<\/code> with a tuple with one element <code>(42,)<\/code> as a right operand and the number of repetitions of this element as a left operand. For example, the expression <code>(42,) * n<\/code> creates the tuple <code>(42, 42, 42, 42, 42)<\/code> for <code>n=5<\/code>.<\/p>\n<p>Let&#8217;s play with an interactive code shell before you&#8217;ll dive into the detailed solution!<\/p>\n<p> <iframe loading=\"lazy\" src=\"https:\/\/trinket.io\/embed\/python\/2f2185e99e\" width=\"100%\" height=\"356\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" allowfullscreen><\/iframe> <\/p>\n<p><em><strong>Exercise<\/strong>: Initialize the tuple with <code>n=20<\/code> placeholder elements <code>-1<\/code> and run the code. <\/em><\/p>\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n<h2>Problem Formulation<\/h2>\n<p>Next, you&#8217;ll learn about the more formal problem and dive into the step-by-step solution.<\/p>\n<p><strong>Problem<\/strong>: Given an integer <code>n<\/code>. How to initialize a tuple with <code>n<\/code> placeholder elements (e.g., <code>42<\/code>)?<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># n=0 --> ()\n# n=1 --> (42,)\n# n=5 --> (42, 42, 42, 42, 42)<\/pre>\n<h2>Example 1 &#8211; Tuple Concatenation<\/h2>\n<p>Use the tuple concatenation operation <code>*<\/code> with a tuple with one element <code>(42,)<\/code> as right operand and the number of repetitions of this element as left operand. For example, the expression <code>(42,) * n<\/code> creates the tuple <code>(42, 42, 42, 42, 42)<\/code> for <code>n=5<\/code>.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"2\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">n = 5\nt = (42,) * n\nprint(t)\n# (42, 42, 42, 42, 42)<\/pre>\n<p>Note that you cannot change the values of a tuple, once created, because unlike lists tuples are <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/mutable-vs-immutable-objects-in-python\/\" data-type=\"post\" data-id=\"204090\" target=\"_blank\">immutable<\/a>. For example, trying to overwrite the third tuple value will yield a <code>TypeError: 'tuple' object does not support item assignment<\/code>.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"2,6\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">>>> x = (42,) * 5\n>>> x[0] = 'Alice'\nTraceback (most recent call last): File \"&lt;pyshell#6>\", line 1, in &lt;module> x[0] = 'Alice'\nTypeError: 'tuple' object does not support item assignment<\/pre>\n<h2>Example 2 &#8211; N-Ary Tuple Concatenation<\/h2>\n<p class=\"has-global-color-8-background-color has-background\">You can also use a generalization of the <strong><em>unary<\/em><\/strong> tuple concatenation &#8212; I call it <strong><em>n-ary tuple concatenation<\/em><\/strong> &#8212; to create a tuple of size <code>n<\/code>. For example, given a tuple <code>t<\/code> of size 3, you can create a tuple of size 9 by multiplying it with the integer 3 like so: <code>t * 3<\/code>. <\/p>\n<p>Here&#8217;s an example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"2\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">simple_tuple = ('Alice', 42, 3.14)\ncomplex_tuple = simple_tuple * 3 print(complex_tuple)\n# ('Alice', 42, 3.14, 'Alice', 42, 3.14, 'Alice', 42, 3.14)<\/pre>\n<h2>Example 3 &#8211; Tuple From List<\/h2>\n<p class=\"has-global-color-8-background-color has-background\">This approach is simple: First, create a list of size n. Second, pass that list into the <code><a href=\"https:\/\/blog.finxter.com\/python-tuple\/\" data-type=\"post\" data-id=\"21575\" target=\"_blank\" rel=\"noreferrer noopener\">tuple()<\/a><\/code> function to create a tuple of size <code>n<\/code>. <\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">n = 100 # 1. Create list of size n\nlst = [42] * n # 2. Change value in (mutable) list\nlst[2] = 'Alice' # 3. Create tuple from list AFTER modification\nt = tuple(lst) # 4. Print tuple\nprint(t)\n# (42, 42, 'Alice', 42, 42, ...)<\/pre>\n<p class=\"has-base-background-color has-background\"><strong>Recommended Tutorial<\/strong>: <a href=\"https:\/\/blog.finxter.com\/how-to-create-a-python-list-of-size-n\/\" data-type=\"post\" data-id=\"10466\">Create a List <\/a><a href=\"https:\/\/blog.finxter.com\/how-to-create-a-python-list-of-size-n\/\" data-type=\"post\" data-id=\"10466\" target=\"_blank\" rel=\"noreferrer noopener\">o<\/a><a href=\"https:\/\/blog.finxter.com\/how-to-create-a-python-list-of-size-n\/\" data-type=\"post\" data-id=\"10466\">f Size n<\/a><\/p>\n<\/p>\n<h2>Example 4 &#8211; Generator Expression (List Comprehension)<\/h2>\n<p class=\"has-global-color-8-background-color has-background\">You can pass a <a href=\"https:\/\/blog.finxter.com\/python-one-line-generator\/\" data-type=\"post\" data-id=\"13194\" target=\"_blank\" rel=\"noreferrer noopener\">generator expression<\/a> into Python&#8217;s built-in <code><a href=\"https:\/\/blog.finxter.com\/python-tuple\/\" data-type=\"post\" data-id=\"21575\" target=\"_blank\" rel=\"noreferrer noopener\">tuple()<\/a><\/code> function to dynamically create a tuple of elements, given another iterable. For example, the expression <code>tuple(i**2 for i in range(10))<\/code> creates a tuple with ten square numbers.<\/p>\n<p>Here&#8217;s the code snippet for copy&amp;paste:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">x = tuple(i**2 for i in range(10))\nprint(x)\n# (0, 1, 4, 9, 16, 25, 36, 49, 64, 81)<\/pre>\n<p>In case you need some background on this terrific Python feature, check out my article on <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/list-comprehension\/\" data-type=\"post\" data-id=\"1171\" target=\"_blank\">List Comprehension<\/a> and my <a rel=\"noreferrer noopener\" href=\"https:\/\/pythononeliners.com\/\" data-type=\"URL\" data-id=\"https:\/\/pythononeliners.com\/\" target=\"_blank\">best-selling<\/a> Python textbook on writing super condensed and concise Python code:<\/p>\n<h2>Python One-Liners Book: Master the Single Line First!<\/h2>\n<p><strong>Python programmers will improve their computer science skills with these useful one-liners.<\/strong><\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-medium is-resized\"><a href=\"https:\/\/www.amazon.com\/gp\/product\/B07ZY7XMX8\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/06\/3D_cover-1024x944.jpg\" alt=\"Python One-Liners\" class=\"wp-image-10007\" width=\"512\" height=\"472\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/06\/3D_cover-scaled.jpg 1024w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/06\/3D_cover-300x277.jpg 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/06\/3D_cover-768x708.jpg 768w\" sizes=\"auto, (max-width: 512px) 100vw, 512px\" \/><\/a><\/figure>\n<\/div>\n<p><a href=\"https:\/\/amzn.to\/2WAYeJE\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"https:\/\/amzn.to\/2WAYeJE\"><em>Python One-Liners<\/em> <\/a>will teach you how to read and write &#8220;one-liners&#8221;: <strong><em>concise statements of useful functionality packed into a single line of code. <\/em><\/strong>You&#8217;ll learn how to systematically unpack and understand any line of Python code, and write eloquent, powerfully compressed Python like an expert.<\/p>\n<p>The book&#8217;s five chapters cover (1) tips and tricks, (2) regular expressions, (3) machine learning, (4) core data science topics, and (5) useful algorithms. <\/p>\n<p>Detailed explanations of one-liners introduce <strong><em>key computer science concepts <\/em><\/strong>and<strong><em> boost your coding and analytical skills<\/em><\/strong>. You&#8217;ll learn about advanced Python features such as <em><strong>list comprehension<\/strong><\/em>, <strong><em>slicing<\/em><\/strong>, <strong><em>lambda functions<\/em><\/strong>, <strong><em>regular expressions<\/em><\/strong>, <strong><em>map <\/em><\/strong>and <strong><em>reduce <\/em><\/strong>functions, and <strong><em>slice assignments<\/em><\/strong>. <\/p>\n<p>You&#8217;ll also learn how to:<\/p>\n<ul>\n<li>Leverage data structures to <strong>solve real-world problems<\/strong>, like using Boolean indexing to find cities with above-average pollution<\/li>\n<li>Use <strong>NumPy basics<\/strong> such as <em>array<\/em>, <em>shape<\/em>, <em>axis<\/em>, <em>type<\/em>, <em>broadcasting<\/em>, <em>advanced indexing<\/em>, <em>slicing<\/em>, <em>sorting<\/em>, <em>searching<\/em>, <em>aggregating<\/em>, and <em>statistics<\/em><\/li>\n<li>Calculate basic <strong>statistics <\/strong>of multidimensional data arrays and the K-Means algorithms for unsupervised learning<\/li>\n<li>Create more <strong>advanced regular expressions<\/strong> using <em>grouping <\/em>and <em>named groups<\/em>, <em>negative lookaheads<\/em>, <em>escaped characters<\/em>, <em>whitespaces, character sets<\/em> (and <em>negative characters sets<\/em>), and <em>greedy\/nongreedy operators<\/em><\/li>\n<li>Understand a wide range of <strong>computer science topics<\/strong>, including <em>anagrams<\/em>, <em>palindromes<\/em>, <em>supersets<\/em>, <em>permutations<\/em>, <em>factorials<\/em>, <em>prime numbers<\/em>, <em>Fibonacci <\/em>numbers, <em>obfuscation<\/em>, <em>searching<\/em>, and <em>algorithmic sorting<\/em><\/li>\n<\/ul>\n<p>By the end of the book, you&#8217;ll know how to <strong><em>write Python at its most refined<\/em><\/strong>, and create concise, beautiful pieces of &#8220;Python art&#8221; in merely a single line.<\/p>\n<p><strong><a href=\"https:\/\/amzn.to\/2WAYeJE\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"https:\/\/amzn.to\/2WAYeJE\"><em>Get your Python One-Liners on Amazon!!<\/em><\/a><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>5\/5 &#8211; (1 vote) Use the tuple concatenation operation * with a tuple with one element (42,) as a right operand and the number of repetitions of this element as a left operand. For example, the expression (42,) * n creates the tuple (42, 42, 42, 42, 42) for n=5. Let&#8217;s play with an interactive [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-127486","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/127486","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=127486"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/127486\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=127486"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=127486"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=127486"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}