HomeLinuxPython Subsequent() Operate

Python Subsequent() Operate


Python is a flexible language that gives many built-in features to simplify the coding methodologies such because the “subsequent()” operate, which is utilized to iterate over/by the weather of an iterable object. This text will exhibit how this operate works, what it may well do, and why it’s helpful utilizing numerous examples.

What’s the “subsequent()” Operate in Python?

Python is an object-oriented programming language used/utilized to create functions, software program, and different digital merchandise. The built-in “subsequent()” operate is a great tool that helps to iterate by an iterable object in Python. This information will clarify the “subsequent()” operate intimately, together with its syntax, parameters, return worth, and examples.

Syntax

 

Parameters

The “subsequent()” operate takes the next two parameters:

  • The “iterable” parameter is an object that must be traversed.
  • The “default” parameter is a worth that’s returned if the iterable has reached its finish. If the “default” parameter will not be specified, the “StopIteration” exception is raised.

Return Worth

The “subsequent()” operate shows the following merchandise/factor from an iterable object. Within the absence of any extra components/gadgets, it raises the “StopIteration” error.

Instance 1: Making use of the “subsequent()” Operate Upon a Record

Let’s overview the next instance:

list_value = [51, 52, 43]
my_iter = iter(list_value)
print(subsequent(my_iter))
print(subsequent(my_iter))
print(subsequent(my_iter))
print(subsequent(my_iter))

 

Within the above instance:

  • The listing named “list_value” is created with three components.
  • After that, create an iterator “my_iter” for the listing utilizing the “iter()” operate.
  • Lastly, the “subsequent()” operate is utilized repeatedly to iterate by/over the enter listing and print/present every merchandise.
  • Upon the top of components/gadgets to be printed, the “StopIteration” exception/error is launched.

Output

Within the above snippet, every merchandise of the listing has been displayed utilizing the “subsequent()” operate and the mentioned exception is raised upon the iteration being accomplished.

Instance 2: Making use of the “subsequent()” Operate Upon a String

This instance illustrates the usage of the “subsequent()” operate upon a string:

string_value = “Python”
iter_value = iter(string_value)
print(subsequent(iter_value))
print(subsequent(iter_value))
print(subsequent(iter_value))
print(subsequent(iter_value))
print(subsequent(iter_value))
print(subsequent(iter_value))
print(subsequent(iter_value))

 

Primarily based on the above code, carry out the next steps:

  • The string named “string_value” is created.
  • An iterator identify “iter_value” is created for the string that applies the “iter()” operate.
  • Lastly, the “subsequent()” operate is applied repeatedly to iterate by the string and print every string character.
  • Likewise, the “StopIteration” exception is raised when there aren’t any extra characters to be printed.

Output

Within the above snippet, every iterated worth is displayed till the final string character is reached.

Instance 3: Making use of the “subsequent()” Operate With a Default Worth

Under is an instance of using the “subsequent()” operate with a default worth:

list_value = [25, 35, 45]
iter_value = iter(list_value)
print(subsequent(iter_value, 0))
print(subsequent(iter_value, 0))
print(subsequent(iter_value, 0))
print(subsequent(iter_value, 0))

 

On this code block:

  • Equally, the listing is initialized and an iterator is created utilizing the “iter()” operate.
  • The “subsequent()” operate iterates by the listing and prints every merchandise step-by-step.
  • It’s such that when there aren’t any extra gadgets to be printed, the default worth of “0” is returned.

Output

The above snippet shows the listing components and returns the default worth i.e., “0” when no components are left.

Conclusion

To iterate by an iterable object in Python, we are able to use the built-in “subsequent()” operate. It provides us the following merchandise from the iterable and stops with the “StopIteration” exception when there aren’t any extra gadgets left. We are able to use the “subsequent()” operate to simply loop over a listing, string, or another iterable object in Python. This weblog demonstrated the utilization and dealing of the “subsequent()” operate in Python.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments