{"id":24841,"date":"2021-11-02T09:54:30","date_gmt":"2021-11-02T04:24:30","guid":{"rendered":"https:\/\/python-programs.com\/?p=24841"},"modified":"2021-11-02T09:54:30","modified_gmt":"2021-11-02T04:24:30","slug":"python-next-function-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-next-function-with-examples\/","title":{"rendered":"Python next() Function with Examples"},"content":{"rendered":"
next() Function in Python:<\/strong><\/p>\n The next() function returns the iterator’s next item.<\/p>\n You can specify a default return value to be returned if the iterable has reached its limit or end.<\/p>\n Syntax:<\/strong><\/p>\n Parameters<\/strong><\/p>\n iterable:<\/strong> This is required. It is an iterable object.<\/p>\n default:<\/strong> This is Optional. If the iterable has reached its limit or end, this is the default value to return.<\/p>\n Return Value:\u00a0<\/strong><\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" next() Function in Python: The next() function returns the iterator’s next item. You can specify a default return value to be returned if the iterable has reached its limit or end. Syntax: next(iterable, default) Parameters iterable: This is required. It is an iterable object. default: This is Optional. If the iterable has reached its limit …<\/p>\nnext(iterable, default)<\/pre>\n
\n
Given List = ['good', 'morning', 'btechgeeks']<\/pre>\n
good\r\nmorning\r\nbtechgeeks\r\nTraceback (most recent call last):\r\nFile \"jdoodle.py\", line 13, in <module>\r\nprint(next(iteratr))\r\nStopIteration<\/pre>\n
Here it prints the iterator value of the list element and increase the \r\niterator to next element. \r\nIt raises an errors when it exceeds the length of the list.<\/pre>\n
Given List = ['good', 'morning', 'btechgeeks']\r\ndefault value = hello<\/pre>\n
good\r\nmorning\r\nbtechgeeks\r\nhello<\/pre>\n
Here it prints the iterator value of the list element and increase the \r\niterator to next element. \r\nWhen it exceeds the length of the list it prints the default value.<\/pre>\n
next() Function with Examples in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
1)Without default value<\/h5>\n
\n
# Give the list as static input and store it in a variable.\r\ngvn_lst = ['good', 'morning', 'btechgeeks']\r\n# Convert the given list to an iterator using the iter() function and\r\n# store it in another variable.\r\niteratr = iter(gvn_lst)\r\n# Apply the next() function to the above iterator to get the next item in the\r\n# given list.\r\n# Store it in another variable.\r\nprint(next(iteratr))\r\n# Similarly, do the same till the end of the list.\r\nprint(next(iteratr))\r\nprint(next(iteratr))\r\nprint(next(iteratr))\r\n<\/pre>\n
good\r\nmorning\r\nbtechgeeks\r\nTraceback (most recent call last):\r\nFile \"jdoodle.py\", line 13, in <module>\r\nprint(next(iteratr))\r\nStopIteration<\/pre>\n
Here it prints the iterator value of the list element and increase iterator to\r\nnext element. \r\nIt raises an errors when it exceeds the length of the list.<\/pre>\n
2)With the default value<\/h5>\n
\n
# Give the list as static input and store it in a variable.\r\ngvn_lst = ['good', 'morning', 'btechgeeks']\r\n# Give the default value as static input and store it in another variable.\r\ngvn_defalt_valu = 'hello'\r\n# Convert the given list to an iterator using the iter() function and\r\n# store it in another variable.\r\niteratr = iter(gvn_lst)\r\n# Pass the above iterator and default value as the arguments to the given list to\r\n# get the next item in the given list.\r\n# Store it in another variable.\r\nprint(next(iteratr, gvn_defalt_valu))\r\n# Similarly, do the same till the end of the list.\r\nprint(next(iteratr, gvn_defalt_valu))\r\nprint(next(iteratr, gvn_defalt_valu))\r\nprint(next(iteratr, gvn_defalt_valu))\r\n<\/pre>\n
good\r\nmorning\r\nbtechgeeks\r\nhello<\/pre>\n
Here it prints the iterator value of the list element and increase iterator to\r\nnext element. \r\nWhen it exceeds the length of the list it prints the default value.<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
1)Without default value<\/h5>\n
\n
# Give the list as user input using list(),map(),input(),and split() functions.\r\n# Store it in a variable.\r\ngvn_lst = list(map(int, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n# Convert the given list to an iterator using the iter() function and\r\n# store it in another variable.\r\niteratr = iter(gvn_lst)\r\n# Apply the next() function to the above iterator to get the next item in the\r\n# given list.\r\n# Store it in another variable.\r\nprint(next(iteratr))\r\n# Similarly, do the same till the end of the list.\r\nprint(next(iteratr))\r\nprint(next(iteratr))\r\nprint(next(iteratr))\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 10 20 30\r\n10\r\n20\r\n30\r\nTraceback (most recent call last):\r\nFile \"jdoodle.py\", line 15, in <module>\r\nprint(next(iteratr))\r\nStopIteration<\/pre>\n
2)With the default value<\/h5>\n
\n
# Give the list as user input using list(),map(),input(),and split() functions.\r\n# Store it in a variable.\r\ngvn_lst = list(map(int, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n# Give the default value as a user input using the int(input()) function and \r\n# store it in another variable.\r\ngvn_defalt_valu = int(input(\"Enter some random number = \"))\r\n# Convert the given list to an iterator using the iter() function and\r\n# store it in another variable.\r\niteratr = iter(gvn_lst)\r\n# Pass the above iterator and default value as the arguments to the given list to\r\n# get the next item in the given list.\r\n# Store it in another variable.\r\nprint(next(iteratr, gvn_defalt_valu))\r\n# Similarly, do the same till the end of the list.\r\nprint(next(iteratr, gvn_defalt_valu))\r\nprint(next(iteratr, gvn_defalt_valu))\r\nprint(next(iteratr, gvn_defalt_valu))\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 10 20 30 \r\nEnter some random number = 40\r\n10\r\n20\r\n30\r\n40<\/pre>\n