Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the item currently pointed at by iterator without incrementing?

Tags:

Is there a way to get the item pointed at by an iterator in python without incrementing the iterator itself? For example how would I implement the following with iterators:

looking_for = iter(when_to_change_the_mode) for l in listA:     do_something(looking_for.current())     if l == looking_for.current():         next(looking_for) 
like image 961
fakedrake Avatar asked Jul 02 '12 14:07

fakedrake


People also ask

What method allows us to get the next item from an iterator object?

We use the next() function to manually iterate through all the items of an iterator.

How do I find the previous value of an iterator?

The previous index and next index in an ArrayList can be obtained using the methods previousIndex() and nextIndex() respectively of the ListIterator interface.

Which of the following methods can be used on an object that is an iterator to iterate through a sequence?

Iterator in python is an object that is used to iterate over iterable objects like lists, tuples, dicts, and sets. The iterator object is initialized using the iter() method. It uses the next() method for iteration.

What is next ITER in Python?

The next() function returns the next item in an iterator. You can add a default return value, to return if the iterable has reached to its end.


1 Answers

Iterators don't have a way to get the current value. If you want that, keep a reference to it yourself, or wrap your iterator to hold onto it for you.

like image 141
Ned Batchelder Avatar answered Oct 06 '22 21:10

Ned Batchelder