[Tut] Python Find in List [Ultimate Guide] - Printable Version +- Sick Gaming (https://www.sickgaming.net) +-- Forum: Programming (https://www.sickgaming.net/forum-76.html) +--- Forum: Python (https://www.sickgaming.net/forum-83.html) +--- Thread: [Tut] Python Find in List [Ultimate Guide] (/thread-100375.html) |
[Tut] Python Find in List [Ultimate Guide] - xSicKxBot - 12-09-2022 Python Find in List [Ultimate Guide] <div> <div class="kk-star-ratings kksr-auto kksr-align-left kksr-valign-top" data-payload='{"align":"left","id":"963784","slug":"default","valign":"top","ignore":"","reference":"auto","class":"","count":"1","legendonly":"","readonly":"","score":"5","best":"5","gap":"5","greet":"Rate this post","legend":"5\/5 - (1 vote)","size":"24","width":"142.5","_legend":"{score}\/{best} - ({count} {votes})","font_factor":"1.25"}'> <div class="kksr-stars"> <div class="kksr-stars-inactive"> <div class="kksr-star" data-star="1" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" data-star="2" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" data-star="3" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" data-star="4" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" data-star="5" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> </p></div> <div class="kksr-stars-active" style="width: 142.5px;"> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> </p></div> </div> <div class="kksr-legend" style="font-size: 19.2px;"> 5/5 – (1 vote) </div> </div> <div class="wp-block-image"> <figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="625" height="938" src="https://blog.finxter.com/wp-content/uploads/2022/12/image-121.png" alt="" class="wp-image-963838" srcset="https://blog.finxter.com/wp-content/uploads/2022/12/image-121.png 625w, https://blog.finxter.com/wp-content/uploads/2022/12/image-121-200x300.png 200w" sizes="(max-width: 625px) 100vw, 625px" /></figure> </div> <p>When Google was founded in 1998, Wallstreet investors laughed at their bold vision of <strong><em>finding data efficiently in the web</em></strong>. Very few people actually believed that finding things can be at the heart of a sustainable business — let alone be a long-term challenge worth pursuing. </p> <p>We have learned that <strong><em>searching</em></strong> — and <strong><em>finding </em></strong>— things is crucial wherever <strong>data volumes exceed processing capabilities</strong>. Every computer scientist knows about the importance of search.</p> <p>And even non-coders don’t laugh about Google’s mission anymore!</p> <p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/2b50.png" alt="⭐" class="wp-smiley" style="height: 1em; max-height: 1em;" /><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/2b50.png" alt="⭐" class="wp-smiley" style="height: 1em; max-height: 1em;" /><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/2b50.png" alt="⭐" class="wp-smiley" style="height: 1em; max-height: 1em;" /> This article will be the web’s most comprehensive guide on <strong>FINDING stuff in a Python list.</strong> <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/2b50.png" alt="⭐" class="wp-smiley" style="height: 1em; max-height: 1em;" /><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/2b50.png" alt="⭐" class="wp-smiley" style="height: 1em; max-height: 1em;" /><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/2b50.png" alt="⭐" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p> <p>It’s a living document where I’ll update new topics as I go along — so stay tuned while this article grows to be the biggest resource on this topic on the whole web!</p> <p>Let’s get started with the very basics of finding stuff in a <a href="https://blog.finxter.com/python-lists/" data-type="post" data-id="7332" target="_blank" rel="noreferrer noopener">Python list</a>:</p> <h2>Finding an Element in a List Using the Membership Operator</h2> <p class="has-global-color-8-background-color has-background">You can use the <a href="https://blog.finxter.com/python-membership-in-operator/" data-type="post" data-id="34005" target="_blank" rel="noreferrer noopener">membership keyword</a> operator <code>in</code> to check if an element is present in a given <a href="https://blog.finxter.com/python-lists/" data-type="post" data-id="7332" target="_blank" rel="noreferrer noopener">list</a>. For example, <code>x in mylist</code> returns <code>True</code> if element <code>x</code> is present in <code>my_list</code> using the <a href="https://blog.finxter.com/python-equal-to/" data-type="post" data-id="31075" target="_blank" rel="noreferrer noopener">equality</a> <code>==</code> operator to compare all list elements against the element <code>x</code> to be found.</p> <p>Here’s a minimal example:</p> <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="">my_list = ['Alice', 'Bob', 'Sergey', 'Larry', 'Eric', 'Sundar'] if 'Eric' in my_list: print('Eric is in the list') </pre> <p>The output is:</p> <pre class="wp-block-preformatted"><code>Eric is in the list</code></pre> <p>Here’s a graphical depiction of how the membership operator works on a list of numbers:</p> <div class="wp-block-image"> <figure class="aligncenter size-full"><img decoding="async" loading="lazy" width="1024" height="576" src="https://blog.finxter.com/wp-content/uploads/2022/12/image-120.png" alt="" class="wp-image-963817" srcset="https://blog.finxter.com/wp-content/uploads/2022/12/image-120.png 1024w, https://blog.finxter.com/wp-content/uploads/2022/12/image-120-300x169.png 300w, https://blog.finxter.com/wp-content/uploads/2022/12/image-120-768x432.png 768w" sizes="(max-width: 1024px) 100vw, 1024px" /><figcaption class="wp-element-caption"><strong>Figure 1:</strong> Check the membership of item 42 in the list of integers.</figcaption></figure> </div> <p>To dive deeper into this topic, I’d love to see you watch my explainer video on the membership operators here: <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f447.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p> <figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube"><a href="https://blog.finxter.com/python-find-in-list/"><img src="https://blog.finxter.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2FoQkfuBAk9lo%2Fhqdefault.jpg" alt="YouTube Video"></a><figcaption></figcaption></figure></p> </div> https://www.sickgaming.net/blog/2022/12/08/python-find-in-list-ultimate-guide/ |