07-06-2020, 09:09 AM
How to Create a Python List of Size n?
<div><p class="has-luminous-vivid-amber-background-color has-background">To create a list of <code>n</code> placeholder elements, multiply the list of a single placeholder element with <code>n</code>. For example, use <code>[None] * 5</code> to create a list <code>[None, None, None, None, None]</code> with five elements <code>None</code>. You can then overwrite some elements with index assignments. In the example, <code>lst[2] = 42</code> would result in the changed list <code>[None, None, 42, None, None]</code>. </p>
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio">
<div class="wp-block-embed__wrapper">
<div class="ast-oembed-container"><iframe title="How to Create a Python List of Size n?" width="1400" height="788" src="https://www.youtube.com/embed/d_uBZeKtXSo?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></div>
</div>
</figure>
<p>Let’s play with an interactive code shell before you’ll dive into the detailed solution!</p>
<p> <iframe src="https://trinket.io/embed/python/8b63f214b1" marginwidth="0" marginheight="0" allowfullscreen="" width="100%" height="400" frameborder="0"></iframe> </p>
<p><em><strong>Exercise</strong>: Initialize the list with <code>n=20</code> placeholder elements <code>-1</code> and run the code. </em></p>
<hr class="wp-block-separator"/>
<p>Next, you’ll learn about the more formal problem and dive into the step-by-step solution.</p>
<p><strong>Problem</strong>: Given an integer <code>n</code>. How to initialize a list with <code>n</code> placeholder elements?</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""># n=0 --> []
# n=1 --> [None]
# n=5 --> [None, None, None, None, None]</pre>
<p><strong>Solution</strong>: Use the list concatenation operation <code>*</code>. </p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">n = 5
lst = [None] * n
print(lst)
# [None, None, None, None, None]</pre>
<p>You can modify the element <code>n</code> as you like. In subsequent operations, you can overwrite all placeholder <code>None</code> list elements using simple index assignment operations:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">lst[0] = 'Alice'
lst[1] = 0
lst[2] = 42
lst[3] = 12
lst[4] = 'hello'
print(lst)
# ['Alice', 0, 42, 12, 'hello']</pre>
<p>However, there’s a small problem if you want to create a list with mutable objects (such as a<a href="https://blog.finxter.com/python-list-of-lists/" title="Python List of Lists – A Helpful Illustrated Guide to Nested Lists in Python" target="_blank" rel="noreferrer noopener"> list of lists</a>):</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">lst = [[]] * n
print(lst)
# [[], [], [], [], []] lst[2].append(42) print(lst)
# [[42], [42], [42], [42], [42]]</pre>
<p>Changing one list element changes all list elements because all list elements refer to the same list object in memory:</p>
<p> <iframe src="https://pythontutor.com/iframe-embed.html#code=n%20%3D%205%0Alst%20%3D%20%5B%5B%5D%5D%20*%20n%0Aprint%28lst%29%0A%23%20%5B%5B%5D,%20%5B%5D,%20%5B%5D,%20%5B%5D,%20%5B%5D%5D%0A%0Alst%5B2%5D.append%2842%29%0A%0Aprint%28lst%29%0A%23%20%5B%5B42%5D,%20%5B42%5D,%20%5B42%5D,%20%5B42%5D,%20%5B42%5D%5D&codeDivHeight=400&codeDivWidth=350&cumulative=false&curInstr=1&heapPrimitives=nevernest&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false" width="800" height="500" frameborder="0"> </iframe> </p>
<p>The solution is to use list comprehension (<a href="https://blog.finxter.com/list-comprehension/" target="_blank" rel="noreferrer noopener" title="List Comprehension in Python — A Helpful Illustrated Guide">see my detailed blog tutorial on list comprehension for a complete guide</a>):</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">lst = [[] for _ in range(n)]
print(lst)
# [[], [], [], [], []] lst[2].append(42)
print(lst)
# [[], [], [42], [], []]</pre>
<p>In the following visualization, you can see how each element now refers to an independent list object in memory:</p>
<p> <iframe src="https://pythontutor.com/iframe-embed.html#code=n%20%3D%205%0Alst%20%3D%20%5B%5B%5D%20for%20_%20in%20range%28n%29%5D%0Aprint%28lst%29%0A%23%20%5B%5B%5D,%20%5B%5D,%20%5B%5D,%20%5B%5D,%20%5B%5D%5D%0A%0Alst%5B2%5D.append%2842%29%0Aprint%28lst%29%0A%23%20%5B%5B%5D,%20%5B%5D,%20%5B42%5D,%20%5B%5D,%20%5B%5D%5D&codeDivHeight=400&codeDivWidth=350&cumulative=false&curInstr=0&heapPrimitives=nevernest&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false" width="800" height="500" frameborder="0"> </iframe> </p>
<p><em><strong>Exercise</strong>: Run the visualization and convince yourself that only one element is modified! Why is this the case?</em></p>
<h2>Where to Go From Here?</h2>
<p>Enough theory, let’s get some practice!</p>
<p>To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?</p>
<p><strong>Practice projects is how you sharpen your saw in coding!</strong></p>
<p>Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?</p>
<p>Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.</p>
<p>Join my free webinar <a rel="noreferrer noopener" href="https://blog.finxter.com/webinar-freelancer/" target="_blank">“How to Build Your High-Income Skill Python”</a> and watch how I grew my coding business online and how you can, too—from the comfort of your own home.</p>
<p><a href="https://blog.finxter.com/webinar-freelancer/" target="_blank" rel="noreferrer noopener">Join the free webinar now!</a></p>
</div>
https://www.sickgaming.net/blog/2020/07/...of-size-n/
<div><p class="has-luminous-vivid-amber-background-color has-background">To create a list of <code>n</code> placeholder elements, multiply the list of a single placeholder element with <code>n</code>. For example, use <code>[None] * 5</code> to create a list <code>[None, None, None, None, None]</code> with five elements <code>None</code>. You can then overwrite some elements with index assignments. In the example, <code>lst[2] = 42</code> would result in the changed list <code>[None, None, 42, None, None]</code>. </p>
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio">
<div class="wp-block-embed__wrapper">
<div class="ast-oembed-container"><iframe title="How to Create a Python List of Size n?" width="1400" height="788" src="https://www.youtube.com/embed/d_uBZeKtXSo?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></div>
</div>
</figure>
<p>Let’s play with an interactive code shell before you’ll dive into the detailed solution!</p>
<p> <iframe src="https://trinket.io/embed/python/8b63f214b1" marginwidth="0" marginheight="0" allowfullscreen="" width="100%" height="400" frameborder="0"></iframe> </p>
<p><em><strong>Exercise</strong>: Initialize the list with <code>n=20</code> placeholder elements <code>-1</code> and run the code. </em></p>
<hr class="wp-block-separator"/>
<p>Next, you’ll learn about the more formal problem and dive into the step-by-step solution.</p>
<p><strong>Problem</strong>: Given an integer <code>n</code>. How to initialize a list with <code>n</code> placeholder elements?</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""># n=0 --> []
# n=1 --> [None]
# n=5 --> [None, None, None, None, None]</pre>
<p><strong>Solution</strong>: Use the list concatenation operation <code>*</code>. </p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">n = 5
lst = [None] * n
print(lst)
# [None, None, None, None, None]</pre>
<p>You can modify the element <code>n</code> as you like. In subsequent operations, you can overwrite all placeholder <code>None</code> list elements using simple index assignment operations:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">lst[0] = 'Alice'
lst[1] = 0
lst[2] = 42
lst[3] = 12
lst[4] = 'hello'
print(lst)
# ['Alice', 0, 42, 12, 'hello']</pre>
<p>However, there’s a small problem if you want to create a list with mutable objects (such as a<a href="https://blog.finxter.com/python-list-of-lists/" title="Python List of Lists – A Helpful Illustrated Guide to Nested Lists in Python" target="_blank" rel="noreferrer noopener"> list of lists</a>):</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">lst = [[]] * n
print(lst)
# [[], [], [], [], []] lst[2].append(42) print(lst)
# [[42], [42], [42], [42], [42]]</pre>
<p>Changing one list element changes all list elements because all list elements refer to the same list object in memory:</p>
<p> <iframe src="https://pythontutor.com/iframe-embed.html#code=n%20%3D%205%0Alst%20%3D%20%5B%5B%5D%5D%20*%20n%0Aprint%28lst%29%0A%23%20%5B%5B%5D,%20%5B%5D,%20%5B%5D,%20%5B%5D,%20%5B%5D%5D%0A%0Alst%5B2%5D.append%2842%29%0A%0Aprint%28lst%29%0A%23%20%5B%5B42%5D,%20%5B42%5D,%20%5B42%5D,%20%5B42%5D,%20%5B42%5D%5D&codeDivHeight=400&codeDivWidth=350&cumulative=false&curInstr=1&heapPrimitives=nevernest&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false" width="800" height="500" frameborder="0"> </iframe> </p>
<p>The solution is to use list comprehension (<a href="https://blog.finxter.com/list-comprehension/" target="_blank" rel="noreferrer noopener" title="List Comprehension in Python — A Helpful Illustrated Guide">see my detailed blog tutorial on list comprehension for a complete guide</a>):</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">lst = [[] for _ in range(n)]
print(lst)
# [[], [], [], [], []] lst[2].append(42)
print(lst)
# [[], [], [42], [], []]</pre>
<p>In the following visualization, you can see how each element now refers to an independent list object in memory:</p>
<p> <iframe src="https://pythontutor.com/iframe-embed.html#code=n%20%3D%205%0Alst%20%3D%20%5B%5B%5D%20for%20_%20in%20range%28n%29%5D%0Aprint%28lst%29%0A%23%20%5B%5B%5D,%20%5B%5D,%20%5B%5D,%20%5B%5D,%20%5B%5D%5D%0A%0Alst%5B2%5D.append%2842%29%0Aprint%28lst%29%0A%23%20%5B%5B%5D,%20%5B%5D,%20%5B42%5D,%20%5B%5D,%20%5B%5D%5D&codeDivHeight=400&codeDivWidth=350&cumulative=false&curInstr=0&heapPrimitives=nevernest&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false" width="800" height="500" frameborder="0"> </iframe> </p>
<p><em><strong>Exercise</strong>: Run the visualization and convince yourself that only one element is modified! Why is this the case?</em></p>
<h2>Where to Go From Here?</h2>
<p>Enough theory, let’s get some practice!</p>
<p>To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?</p>
<p><strong>Practice projects is how you sharpen your saw in coding!</strong></p>
<p>Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?</p>
<p>Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.</p>
<p>Join my free webinar <a rel="noreferrer noopener" href="https://blog.finxter.com/webinar-freelancer/" target="_blank">“How to Build Your High-Income Skill Python”</a> and watch how I grew my coding business online and how you can, too—from the comfort of your own home.</p>
<p><a href="https://blog.finxter.com/webinar-freelancer/" target="_blank" rel="noreferrer noopener">Join the free webinar now!</a></p>
</div>
https://www.sickgaming.net/blog/2020/07/...of-size-n/