{"id":4776,"date":"2022-09-15T01:33:15","date_gmt":"2022-09-15T01:33:15","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=4776"},"modified":"2022-09-15T01:33:50","modified_gmt":"2022-09-15T01:33:50","slug":"python-next","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/python-built-in-functions\/python-next\/","title":{"rendered":"Python next() function"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to use the Python <code>next()<\/code> function and how to use it to retrieve the next item from an iterator.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-python-next-function'>Introduction to the Python next() function <a href=\"#introduction-to-the-python-next-function\" class=\"anchor\" id=\"introduction-to-the-python-next-function\" title=\"Anchor for Introduction to the Python next() function\">#<\/a><\/h2>\n\n\n\n<p>An <a href=\"https:\/\/www.pythontutorial.net\/advanced-python\/python-iterators\/\">iterator<\/a> is an object that implements the <em>iterator protocol <\/em>which consists of two methods:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>__iter__()<\/code> method that returns the iterator object itself.<\/li><li><code><code>__next__()<\/code><\/code> method that returns the next item. If all items have been returned, the <code><code>__next__()<\/code><\/code> method raises a <code>StopIteration<\/code> exception.<\/li><\/ul>\n\n\n\n<p>The <code>next()<\/code> function get the next item from an iterator by calling its <code>__next__()<\/code> method. <\/p>\n\n\n\n<p>Here&#8217;s the syntax of the <code>next()<\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">next(iterator&#91;, default])<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>next()<\/code> function has two parameters:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>iterator<\/code> &#8211; this required argument specifies an iterator from which you want to retrieve the next item.<\/li><li><code>default<\/code> &#8211; this is an optional argument. The <code>next()<\/code> function returns the default if the iterator is exhausted; otherwise, it&#8217;ll raise a <code>StopIteration<\/code> exception.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='python-next-function-examples'>Python next() function examples <a href=\"#python-next-function-examples\" class=\"anchor\" id=\"python-next-function-examples\" title=\"Anchor for Python next() function examples\">#<\/a><\/h2>\n\n\n\n<p>Let&#8217;s take some examples of using the <code>next()<\/code> function.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='1-using-python-next-function-with-an-iterator-example'>1) Using Python next() function with an iterator example <a href=\"#1-using-python-next-function-with-an-iterator-example\" class=\"anchor\" id=\"1-using-python-next-function-with-an-iterator-example\" title=\"Anchor for 1) Using Python next() function with an iterator example\">#<\/a><\/h3>\n\n\n\n<p>The following example defines an iterator that returns a number of random numbers between <code>min<\/code> and <code>max<\/code> and uses the <code>next()<\/code> function to get the next random number from the iterator:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">from<\/span> random <span class=\"hljs-keyword\">import<\/span> randint\n\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">random_seq<\/span><span class=\"hljs-params\">(min, max, size)<\/span>:<\/span>\n    <span class=\"hljs-keyword\">for<\/span> _ <span class=\"hljs-keyword\">in<\/span> range(<span class=\"hljs-number\">0<\/span>, size):\n        <span class=\"hljs-keyword\">yield<\/span> randint(min, max)\n\n\nrandom_numbers = random_seq(<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">100<\/span>, <span class=\"hljs-number\">3<\/span>)\n\nr = next(random_numbers)\nprint(r)\n\nr = next(random_numbers)\nprint(r)\n\nr = next(random_numbers)\nprint(r)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<p>First, import the <code>randint()<\/code> function from the built-in random module:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">from<\/span> random <span class=\"hljs-keyword\">import<\/span> randint<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, define a generator that returns a number of random integers between min and max:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">random_seq<\/span><span class=\"hljs-params\">(min, max, size)<\/span>:<\/span>\n    <span class=\"hljs-keyword\">for<\/span> _ <span class=\"hljs-keyword\">in<\/span> range(<span class=\"hljs-number\">0<\/span>, size):\n        <span class=\"hljs-keyword\">yield<\/span> randint(min, max)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Third, call the random_seq function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">random_numbers = random_seq(<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">100<\/span>, <span class=\"hljs-number\">3<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The random_numbers is a generator which is also an iterator.<\/p>\n\n\n\n<p>Fourth, call the <code>next()<\/code> function three times to get the next item from the random_numbers iterator:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">r = next(random_numbers)\nprint(r)\n\nr = next(random_numbers)\nprint(r)\n\nr = next(random_numbers)\nprint(r)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>It should display three random numbers between 1 and 100.<\/p>\n\n\n\n<p>If you call the <code>next()<\/code> function one more time, you&#8217;ll get a StopIteration exception:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">r = next(random_numbers) <span class=\"hljs-comment\"># StopIteration<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>However, if you use a default value, the <code>next()<\/code> function will return that value instead of raising the StopIteration exception:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">r = next(random_numbers, <span class=\"hljs-literal\">None<\/span>)\nprint(r)  <span class=\"hljs-comment\"># None<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\" id='2-using-python-next-function-to-skip-heading-when-reading-a-csv-file'>2) Using Python next() function to skip heading when reading a csv file <a href=\"#2-using-python-next-function-to-skip-heading-when-reading-a-csv-file\" class=\"anchor\" id=\"2-using-python-next-function-to-skip-heading-when-reading-a-csv-file\" title=\"Anchor for 2) Using Python next() function to skip heading when reading a csv file\">#<\/a><\/h3>\n\n\n\n<p>The following example uses the built-in csv module to <a href=\"https:\/\/www.pythontutorial.net\/country\/\" target=\"_blank\" rel=\"noreferrer noopener\">read the country.csv file<\/a> with the following information:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"650\" height=\"254\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/image.png\" alt=\"\" class=\"wp-image-4777\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/image.png 650w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/image-300x117.png 300w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/figure>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> csv\n\n<span class=\"hljs-keyword\">with<\/span> open(<span class=\"hljs-string\">'country.csv'<\/span>, encoding=<span class=\"hljs-string\">'utf8'<\/span>) <span class=\"hljs-keyword\">as<\/span> f:\n    csv_reader = csv.reader(f)\n\n    <span class=\"hljs-comment\"># skip the header<\/span>\n    next(csv_reader)\n\n    <span class=\"hljs-comment\"># iterate over the data line<\/span>\n    <span class=\"hljs-keyword\">for<\/span> line <span class=\"hljs-keyword\">in<\/span> csv_reader:\n        print(line)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<p>First, import the csv module:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> csv<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, open the country.csv file and return the reader object from the csv.<code>reader()<\/code>  function. The reader object is an iterator.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">with<\/span> open(<span class=\"hljs-string\">'country.csv'<\/span>, encoding=<span class=\"hljs-string\">'utf8'<\/span>) <span class=\"hljs-keyword\">as<\/span> f:\n    csv_reader = csv.reader(f)\n    <span class=\"hljs-comment\">#... <\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Third, call the <code>next()<\/code> function to retrieve the header from the csv_reader iterator:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">next(csv_reader)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Finally, display all the data lines:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">for<\/span> line <span class=\"hljs-keyword\">in<\/span> csv_reader:\n    print(line)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Use the Python <code>next()<\/code> function to get the next item from an iterator.<\/li><\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"4776\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-built-in-functions\/python-next\/\"\n\t\t\t\tdata-post-title=\"Python next() function\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"4776\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-built-in-functions\/python-next\/\"\n\t\t\t\tdata-post-title=\"Python next() function\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you&#8217;ll learn how to use the Python next() function and how to use it to retrieve the next item from an iterator. Introduction to the Python next() function # An iterator is an object that implements the iterator protocol which consists of two methods: __iter__() method that returns the iterator object [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1055,"menu_order":14,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-4776","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/4776","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=4776"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/4776\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1055"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=4776"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}