Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] How to Check if a List Has an Even Number of Elements?

#1
How to Check if a List Has an Even Number of Elements?

<div><h2>Problem Formulation</h2>
<p>Given a <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="post" data-id="7332" target="_blank">list</a> in Python. How to check if the list has an even number of elements?</p>
<p>Examples:</p>
<ul>
<li><code>[] --> True</code></li>
<li><code>[1] --> False</code></li>
<li><code>[1, 2] --> True</code></li>
<li><code>[1, 2, 3] --> False</code></li>
</ul>
<p><strong>Related Article:</strong></p>
<ul>
<li><a href="https://blog.finxter.com/how-to-check-if-a-list-has-an-odd-number-of-elements/" data-type="URL" data-id="https://blog.finxter.com/how-to-check-if-a-list-has-an-odd-number-of-elements/" target="_blank" rel="noreferrer noopener">How to check if a list has an odd number of elements?</a></li>
</ul>
<h2>Method 1: len() and Modulo</h2>
<p class="has-global-color-8-background-color has-background">The most Pythonic way to check if a list has an even number of elements is to use the <a rel="noreferrer noopener" href="https://blog.finxter.com/python-modulo/" data-type="post" data-id="104" target="_blank">modulo expression</a> <code>len(my_list)%2</code> that returns <code>1</code> if the <a href="https://blog.finxter.com/python-len/" data-type="post" data-id="22386">list length</a> is odd and <code>0</code> if the list length is even. So to check if a list has an even number of elements use the expression <code>len(my_list)%2==0</code>.</p>
<p>Here’s a simple code example:</p>
<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="">def check_even(my_list): return len(my_list)%2==0 print(check_even([]))
# True print(check_even([1]))
# False print(check_even([1, 2]))
# True print(check_even([1, 2, 3]))
# False
</pre>
<p>As background, feel free to watch the following video on the modulo operator:</p>
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio">
<div class="wp-block-embed__wrapper">
<iframe loading="lazy" title="Python Modulo - A Simple Illustrated Guide" width="780" height="439" src="https://www.youtube.com/embed/8H5R7eTNOUw?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div>
</figure>
<p>The length function is explained in this video and <a href="https://blog.finxter.com/python-len/" data-type="URL" data-id="https://blog.finxter.com/python-len/" target="_blank" rel="noreferrer noopener">blog article</a>:</p>
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio">
<div class="wp-block-embed__wrapper">
<iframe loading="lazy" title="Python len() -- A Simple Guide" width="780" height="439" src="https://www.youtube.com/embed/WZYnweSv9yI?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div>
</figure>
<p>A slight variant of this method is the following.</p>
<h2>Method 2: len() and Modulo and bool()</h2>
<p class="has-global-color-8-background-color has-background">To check if a list has an even number of elements, you can use the <a rel="noreferrer noopener" href="https://blog.finxter.com/python-modulo/" data-type="post" data-id="104" target="_blank">modulo expression</a> <code>len(my_list)%2</code> that returns <code>1</code> if the <a rel="noreferrer noopener" href="https://blog.finxter.com/python-len/" data-type="post" data-id="22386" target="_blank">list length</a> is odd and <code>0</code> if the list length is even. So to convert the even value 0 to a boolean, use the built-in <code><a rel="noreferrer noopener" href="https://blog.finxter.com/python-bool/" data-type="post" data-id="17841" target="_blank">bool()</a></code> function around the result and invert the result, i.e., <code>not bool(len(my_list)%2)</code>.</p>
<p>Here’s a simple code example:</p>
<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="">def check_even(my_list): return not bool(len(my_list)%2) print(check_even([]))
# True print(check_even([1]))
# False print(check_even([1, 2]))
# True print(check_even([1, 2, 3]))
# False
</pre>
<p>As background, you may want to look at this explainer video:</p>
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio">
<div class="wp-block-embed__wrapper">
<iframe loading="lazy" title="Python bool() - Everything You Need to Know and More" width="780" height="439" src="https://www.youtube.com/embed/qGg_z33J_b4?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div>
</figure>
<h2>Method 3: Bitwise AND</h2>
<p class="has-global-color-8-background-color has-background">You can use the expression <code>len(my_list)&amp;1</code> that uses the <a rel="noreferrer noopener" href="https://blog.finxter.com/python-bitwise-and-operator/" data-type="post" data-id="32148" target="_blank">Bitwise AND operator</a> to return 1 if the list has an even number of elements and 0 otherwise. Now, you simply convert it to a Boolean if needed using the <code>bool()</code> function and invert it using the <code>not</code> operator: <code>not bool(len(my_list)&amp;1)</code>.</p>
<p>Python’s <strong><em>bitwise AND</em></strong> operator <code>x &amp; y</code> performs <em>logical AND</em> on each bit position on the binary representations of integers <code>x</code> and <code>y</code>. Thus, each output bit is 1 if both input bits at the same position are 1, otherwise, it’s 0. </p>
<p>If you run <code>x &amp; 1</code>, Python performs logical and with the bit sequence <code>y=0000...001</code>. For the result, all positions will be <code>0</code> and the last position will be 1 only if <code>x</code>‘s last position is already <code>1</code> which means it is odd. </p>
<p>After converting it using <code>bool()</code>, you still need to invert it using the <a href="https://blog.finxter.com/python-not-operator/" data-type="post" data-id="32000" target="_blank" rel="noreferrer noopener"><code>not</code> operator</a> so that it returns <code>True</code> if the list has an even number of elements.</p>
<p>Here’s an example:</p>
<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="">def check_even(my_list): return not bool(len(my_list)&amp;1) print(check_even([]))
# True print(check_even([1]))
# False print(check_even([1, 2]))
# True print(check_even([1, 2, 3]))
# False
</pre>
<p>Bitwise AND is more efficient than the modulo operator so if performance is an issue for you, you may want to use this third approach.</p>
<p>You may want to watch this video on the Bitwise AND operator:</p>
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio">
<div class="wp-block-embed__wrapper">
<iframe loading="lazy" title="Python Bitwise AND Operator &amp;" width="780" height="439" src="https://www.youtube.com/embed/YeV1FD_YfFs?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div>
</figure>
<h2>Where to Go From Here?</h2>
<p>Enough theory. Let’s get some practice!</p>
<p>Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation. </p>
<p>To become more successful in coding, solve more real problems for real people. 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>You build high-value coding skills by working on practical coding projects!</strong></p>
<p>Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?</p>
<p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/13.1.0/72x72/1f680.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> If your answer is <strong><em>YES!</em></strong>, consider becoming a <a rel="noreferrer noopener" href="https://blog.finxter.com/become-python-freelancer-course/" data-type="page" data-id="2072" target="_blank">Python freelance developer</a>! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.</p>
<p>If you just want to learn about the freelancing opportunity, feel free to watch 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 learn 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/2022/04/...-elements/
Reply



Forum Jump:


Users browsing this thread:
2 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016