Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Python next()

#1
Python next()

<div><p class="has-pale-cyan-blue-background-color has-background">The <code>next(iterator)</code> function is one of Python’s built-in functions—so, you can use it without importing any library. It returns the next value from the <code>iterator</code> you pass as a required first argument. An optional second argument <code>default</code> returns the passed default value in case the iterator doesn’t provide a next value. </p>
<div class="wp-block-image">
<figure class="aligncenter size-large"><img loading="lazy" width="1024" height="576" src="https://blog.finxter.com/wp-content/uploads/2020/11/next_python-1-1024x576.jpg" alt="Python next()" class="wp-image-17152" srcset="https://blog.finxter.com/wp-content/uploads/2020/11/next_python-1-scaled.jpg 1024w, https://blog.finxter.com/wp-content/uplo...00x169.jpg 300w, https://blog.finxter.com/wp-content/uplo...68x432.jpg 768w, https://blog.finxter.com/wp-content/uplo...150x84.jpg 150w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>
<p><strong>Syntax:</strong></p>
<pre class="wp-block-preformatted">next(iterator, &lt;default>)</pre>
<hr class="wp-block-separator"/>
<p><strong>Arguments</strong>:</p>
<ul>
<li><strong>iterator</strong> – the next element is retrieved from the <code>iterator</code></li>
<li><strong>default</strong> (optional) – return value if iterator is exhausted (it doesn’t have a next element)</li>
</ul>
<p><strong>Related Tutorials:</strong></p>
<ul>
<li><a href="https://blog.finxter.com/python-cheat-sheet/" title="Python Beginner Cheat Sheet: 19 Keywords Every Coder Must Know" target="_blank" rel="noreferrer noopener">Python Keywords Cheat Sheet</a></li>
<li><a href="https://wiki.python.org/moin/Iterator" target="_blank" rel="noreferrer noopener" title="https://wiki.python.org/moin/Iterator">Python Iterators</a></li>
<li><a href="https://blog.finxter.com/yield-keyword-in-python-a-simple-illustrated-guide/" title="Yield Keyword in Python – A Simple Illustrated Guide" target="_blank" rel="noreferrer noopener">Yield Keyword</a></li>
<li><a href="https://blog.finxter.com/python-lists/" title="The Ultimate Guide to Python Lists" target="_blank" rel="noreferrer noopener">Python Lists</a></li>
</ul>
<h2>Example 1: No Default Value</h2>
<p>The following example shows the <code>next()</code> function in action—without using a default value in case the iterator is empty. </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="">users = ['Alice', 'Bob', 'Carl', 'David'] # convert the list to an iterator
users_iterator = iter(users) x = next(users_iterator)
print(x)
# Output: 'Alice' x = next(users_iterator)
print(x)
# Output: 'Bob' x = next(users_iterator)
print(x)
# Output: 'Carl' x = next(users_iterator)
print(x)
# Output: 'David'
</pre>
<p>Each time you call <code>next(iterator)</code>, the iterator returns the next element in the iterator over the Python <a href="https://blog.finxter.com/python-lists/" target="_blank" rel="noreferrer noopener" title="The Ultimate Guide to Python Lists">list </a><code>users</code>. </p>
<p>But what happens if you call the <code>next()</code> function once more on the now empty <code>users_iterator</code> object?</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="">x = next(users_iterator)
print(x) '''
Traceback (most recent call last): File "C:\Users\xcent\Desktop\Finxter\Blog\HowToConvertBooleanToStringPython\code.py", line 22, in &lt;module> x = next(users_iterator)
StopIteration '''</pre>
<p>Python throws a <code>StopIteration</code> error. </p>
<p>Let’s learn how to fix this!</p>
<h2>Example 2: With Default Value</h2>
<p>Not providing Python a solution to the problem that the iterator may be empty is a common source of errors! You can fix the errors by passing the optional <code>default</code> argument:</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="">
x = next(users_iterator, 42)
print(x)
# 42
</pre>
<p>Now, you cannot crash the <code>next(...)</code> function anymore! Go ahead and try it…</p>
<h2>Interactive Shell</h2>
<p>The interactive code shell offers you a way to try your newly gained skill—understanding the <code>next()</code> function. Can you crash the script by changing the default value?</p>
<p> <iframe src="https://trinket.io/embed/python/dc149801a3" width="100%" height="356" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> </p>
<p><em><strong>Exercise</strong>: Run the code in the interactive shell. Now, change the default value &amp; run again!</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>
<p>The post <a href="https://blog.finxter.com/python-next/" target="_blank" rel="noopener noreferrer">Python next()</a> first appeared on <a href="https://blog.finxter.com/" target="_blank" rel="noopener noreferrer">Finxter</a>.</p>
</div>


https://www.sickgaming.net/blog/2020/11/21/python-next/
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016