{"id":120993,"date":"2020-11-21T19:03:43","date_gmt":"2020-11-21T19:03:43","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=17135"},"modified":"2020-11-21T19:03:43","modified_gmt":"2020-11-21T19:03:43","slug":"python-next","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2020\/11\/21\/python-next\/","title":{"rendered":"Python next()"},"content":{"rendered":"<p class=\"has-pale-cyan-blue-background-color has-background\">The <code>next(iterator)<\/code> function is one of Python&#8217;s built-in functions&#8212;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&#8217;t provide a next value. <\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img decoding=\"async\" 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\/uploads\/2020\/11\/next_python-1-300x169.jpg 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/11\/next_python-1-768x432.jpg 768w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/11\/next_python-1-150x84.jpg 150w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n<p><strong>Syntax:<\/strong><\/p>\n<pre class=\"wp-block-preformatted\">next(iterator, &lt;default>)<\/pre>\n<hr class=\"wp-block-separator\"\/>\n<p><strong>Arguments<\/strong>:<\/p>\n<ul>\n<li><strong>iterator<\/strong> &#8211; the next element is retrieved from the <code>iterator<\/code><\/li>\n<li><strong>default<\/strong> (optional) &#8211; return value if iterator is exhausted (it doesn&#8217;t have a next element)<\/li>\n<\/ul>\n<p><strong>Related Tutorials:<\/strong><\/p>\n<ul>\n<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>\n<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>\n<li><a href=\"https:\/\/blog.finxter.com\/yield-keyword-in-python-a-simple-illustrated-guide\/\" title=\"Yield Keyword in Python \u2013 A Simple Illustrated Guide\" target=\"_blank\" rel=\"noreferrer noopener\">Yield Keyword<\/a><\/li>\n<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>\n<\/ul>\n<h2>Example 1: No Default Value<\/h2>\n<p>The following example shows the <code>next()<\/code> function in action&#8212;without using a default value in case the iterator is empty. <\/p>\n<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\nusers_iterator = iter(users) x = next(users_iterator)\nprint(x)\n# Output: 'Alice' x = next(users_iterator)\nprint(x)\n# Output: 'Bob' x = next(users_iterator)\nprint(x)\n# Output: 'Carl' x = next(users_iterator)\nprint(x)\n# Output: 'David'\n<\/pre>\n<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>\n<p>But what happens if you call the <code>next()<\/code> function once more on the now empty <code>users_iterator<\/code> object?<\/p>\n<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)\nprint(x) '''\nTraceback (most recent call last): File \"C:\\Users\\xcent\\Desktop\\Finxter\\Blog\\HowToConvertBooleanToStringPython\\code.py\", line 22, in &lt;module> x = next(users_iterator)\nStopIteration '''<\/pre>\n<p>Python throws a <code>StopIteration<\/code> error. <\/p>\n<p>Let&#8217;s learn how to fix this!<\/p>\n<h2>Example 2: With Default Value<\/h2>\n<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>\n<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=\"\">\nx = next(users_iterator, 42)\nprint(x)\n# 42\n<\/pre>\n<p>Now, you cannot crash the <code>next(...)<\/code> function anymore! Go ahead and try it&#8230;<\/p>\n<h2>Interactive Shell<\/h2>\n<p>The interactive code shell offers you a way to try your newly gained skill&#8212;understanding the <code>next()<\/code> function. Can you crash the script by changing the default value?<\/p>\n<p> <iframe loading=\"lazy\" src=\"https:\/\/trinket.io\/embed\/python\/dc149801a3\" width=\"100%\" height=\"356\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" allowfullscreen><\/iframe> <\/p>\n<p><em><strong>Exercise<\/strong>: Run the code in the interactive shell. Now, change the default value &amp; run again!<\/em><\/p>\n<h2>Where to Go From Here?<\/h2>\n<p>Enough theory, let\u2019s get some practice!<\/p>\n<p>To become successful in coding, you need to get out there and solve real problems for real people. That\u2019s how you can become a six-figure earner easily. And that\u2019s how you polish the skills you really need in practice. After all, what\u2019s the use of learning theory that nobody ever needs?<\/p>\n<p><strong>Practice projects is how you sharpen your saw in coding!<\/strong><\/p>\n<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>\n<p>Then become a Python freelance developer! It\u2019s the best way of approaching the task of improving your Python skills\u2014even if you are a complete beginner.<\/p>\n<p>Join my free webinar <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/webinar-freelancer\/\" target=\"_blank\">\u201cHow to Build Your High-Income Skill Python\u201d<\/a> and watch how I grew my coding business online and how you can, too\u2014from the comfort of your own home.<\/p>\n<p><a href=\"https:\/\/blog.finxter.com\/webinar-freelancer\/\" target=\"_blank\" rel=\"noreferrer noopener\">Join the free webinar now!<\/a><\/p>\n<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>\n","protected":false},"excerpt":{"rendered":"<p>The next(iterator) function is one of Python&#8217;s built-in functions&#8212;so, you can use it without importing any library. It returns the next value from the iterator you pass as a required first argument. An optional second argument default returns the passed default value in case the iterator doesn&#8217;t provide a next value. Syntax: next(iterator, &lt;default>) Arguments: [&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-120993","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\/120993","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=120993"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/120993\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=120993"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=120993"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=120993"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}