{"id":3759,"date":"2020-07-30T16:25:13","date_gmt":"2020-07-30T10:55:13","guid":{"rendered":"http:\/\/www.pythonpool.com\/?p=3759"},"modified":"2026-07-13T12:27:39","modified_gmt":"2026-07-13T06:57:39","slug":"python-next","status":"publish","type":"post","link":"https:\/\/www.pythonpool.com\/python-next\/","title":{"rendered":"Python next(): Iterators, Defaults, and StopIteration"},"content":{"rendered":"<p>Python&#8217;s built-in <code>next()<\/code> retrieves one item from an iterator and advances that iterator&#8217;s state. Call <code>next(iterator)<\/code> when exhaustion should raise <code>StopIteration<\/code>, or call <code>next(iterator, default)<\/code> when reaching the end is an ordinary outcome. The function is especially useful for reading one value lazily without materializing an entire collection.<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-transparent ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #990303;color:#990303\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #990303;color:#990303\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 eztoc-toggle-hide-by-default' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.pythonpool.com\/python-next\/#Quick_answer\" >Quick answer<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.pythonpool.com\/python-next\/#Get_items_from_an_iterator\" >Get items from an iterator<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.pythonpool.com\/python-next\/#Handle_StopIteration\" >Handle StopIteration<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.pythonpool.com\/python-next\/#Provide_a_default_value\" >Provide a default value<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.pythonpool.com\/python-next\/#Find_the_first_matching_item_lazily\" >Find the first matching item lazily<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.pythonpool.com\/python-next\/#Understand_custom_iterators\" >Understand custom iterators<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.pythonpool.com\/python-next\/#Choose_next_or_a_loop\" >Choose next or a loop<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.pythonpool.com\/python-next\/#Use_a_sentinel_when_None_is_valid\" >Use a sentinel when None is valid<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/www.pythonpool.com\/python-next\/#Do_not_reuse_an_exhausted_iterator_accidentally\" >Do not reuse an exhausted iterator accidentally<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/www.pythonpool.com\/python-next\/#Keep_StopIteration_inside_the_iterator_boundary\" >Keep StopIteration inside the iterator boundary<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/www.pythonpool.com\/python-next\/#Use_next_with_a_file_or_stream_carefully\" >Use next with a file or stream carefully<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/www.pythonpool.com\/python-next\/#Keep_the_first-match_predicate_pure\" >Keep the first-match predicate pure<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/www.pythonpool.com\/python-next\/#Make_the_exhaustion_policy_visible\" >Make the exhaustion policy visible<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/www.pythonpool.com\/python-next\/#Frequently_Asked_Questions\" >Frequently Asked Questions<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/www.pythonpool.com\/python-next\/#What_does_next_do_in_Python\" >What does next() do in Python?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/www.pythonpool.com\/python-next\/#How_do_I_avoid_StopIteration_with_next\" >How do I avoid StopIteration with next()?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-17\" href=\"https:\/\/www.pythonpool.com\/python-next\/#How_do_I_find_the_first_matching_item\" >How do I find the first matching item?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-18\" href=\"https:\/\/www.pythonpool.com\/python-next\/#What_is_the_difference_between_an_iterable_and_an_iterator\" >What is the difference between an iterable and an iterator?<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"Quick_answer\"><\/span>Quick answer<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Turn an iterable into an iterator with <code>iter()<\/code>, then call <code>next()<\/code>. Use a second argument for a fallback instead of catching <code>StopIteration<\/code> when absence is expected. The official <a href=\"https:\/\/docs.python.org\/3\/library\/functions.html#next\">next() documentation<\/a> defines both forms and the iterator protocol.<\/p>\n<figure class=\"pythonpool-article-visual\"><img src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/python-next-iterator-flow.png\" alt=\"Python next diagram showing iterator state, StopIteration, default values, and lazy first-match filtering\" width=\"1536\" height=\"1024\" loading=\"lazy\" decoding=\"async\"><figcaption>next() consumes iterator state; provide a default or handle StopIteration according to whether exhaustion is expected.<\/figcaption><\/figure>\n<h2><span class=\"ez-toc-section\" id=\"Get_items_from_an_iterator\"><\/span>Get items from an iterator<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>A list is iterable but is not itself an iterator. Call <code>iter()<\/code> to create an iterator, then request values one at a time.<\/p>\n<pre><code class=\"language-python\">values = [\"first\", \"second\"]\niterator = iter(values)\n\nprint(next(iterator))\nprint(next(iterator))\n<\/code><\/pre>\n<p>Each call advances the same iterator. A second name pointing at that iterator observes the same consumed state; creating a new iterator over the list starts from the beginning.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Handle_StopIteration\"><\/span>Handle StopIteration<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>When an iterator has no item left, the one-argument form raises <code>StopIteration<\/code>. A <code>for<\/code> loop handles this protocol internally, which is why ordinary loops do not need a manual exception block.<\/p>\n<pre><code class=\"language-python\">iterator = 