Answers for "next() python"

1

next() python

#next() returns the next number in an iterator
#iter() returns an iterator of the given object

l = ['apple', 'banana', 'cherry', 'grape']
l_iterable = iter(l) #makes the l list iterable

next_value = next(l_iterable) #gets the first value
print(next_value) #prints 'apple'

next_value = next(l_iterable) #gets the next value
print(next_value) #prints 'banana'

next_value = next(l_iterable) #gets the next value
print(next_value) #prints 'cherry'

next_value = next(l_iterable) #gets the next value
print(next_value) #prints 'grape'
Posted by: Guest on April-23-2022

Python Answers by Framework

Browse Popular Code Answers by Language