Questions tagged [iterator]

An iterator is an object-oriented programming pattern that allows traversal through a collection, agnostic of the actual implementation or object addresses in physical memory. It is one of the Gang of Four's behavioral design patterns.

An iterator is an object-oriented programming pattern which for the most part functions similarly to a pointer to an object inside a collection with the added ability to traverse through the collection in a defined manner, agnostic of the actual implementation or even object addresses in physical memory.

The following types of iterators are the most common:

  • Forward
  • Reverse
  • Bidirectional
  • Random Access

Iterators may be read-only, write-only or read-write.

Forward iterators typically have a next
Reverse iterators typically have a previous and
sometimes iterators have a remove method.

This is one of the Gang of Four's behavioral , first published in Gamma et al.'s book "Design Patterns: Elements of Reusable Object-Oriented Software".

The Gang of Four pattern is discussed separately on Wikipedia.

13508 questions
11880
votes
47 answers

What does the "yield" keyword do?

What is the use of the yield keyword in Python? What does it do? For example, I'm trying to understand this code1: def _get_child_candidates(self, distance, min_dist, max_dist): if self._leftchild and distance - max_dist < self._median: …
Alex. S.
  • 134,175
  • 17
  • 51
  • 61
1109
votes
7 answers

How to iterate through two lists in parallel?

I have two iterables in Python, and I want to go over them in pairs: foo = (1, 2, 3) bar = (4, 5, 6) for (f, b) in some_iterator(foo, bar): print("f: ", f, "; b: ", b) It should result in: f: 1; b: 4 f: 2; b: 5 f: 3; b: 6 One way to do it is…
Nathan Fellman
  • 114,610
  • 97
  • 251
  • 311
793
votes
9 answers

How can I iterate over files in a given directory?

I need to iterate through all .asm files inside a given directory and do some actions on them. How can this be done in a efficient way?
Itzik984
  • 14,640
  • 26
  • 68
  • 101
690
votes
13 answers

Difference between Python's Generators and Iterators

What is the difference between iterators and generators? Some examples for when you would use each case would be helpful.
newToProgramming
  • 7,039
  • 3
  • 15
  • 8
650
votes
11 answers

Calling remove in foreach loop in Java

In Java, is it legal to call remove on a collection when iterating through the collection using a foreach loop? For instance: List names = .... for (String name : names) { // Do something names.remove(name). } As an addendum, is it…
Michael Bobick
  • 8,597
  • 5
  • 20
  • 13
636
votes
9 answers

How to build a basic iterator?

How would one create an iterative function (or iterator object) in python?
akdom
  • 29,713
  • 26
  • 71
  • 79
596
votes
6 answers

Iterator invalidation rules for C++ containers

What are the iterator invalidation rules for C++ containers? (Note: This Q&A is an entry in Stack Overflow's C++ FAQ. Meta-discussion about the question itself should be posted on the Meta question that started all of this, not here.)
Lightness Races in Orbit
  • 366,658
  • 71
  • 612
  • 1,016
559
votes
11 answers

How to iterate (keys, values) in JavaScript?

I have a dictionary that has the format of dictionary = {0: {object}, 1:{object}, 2:{object}} How can I iterate through this dictionary by doing something like for ((key, value) in dictionary) { //Do stuff where key would be 0 and value would…
nbroeking
  • 7,110
  • 3
  • 15
  • 38
555
votes
9 answers

How to convert an Iterator to a Stream?

I am looking for a concise way to convert an Iterator to a Stream or more specifically to "view" the iterator as a stream. For performance reason, I would like to avoid a copy of the iterator in a new list: Iterator sourceIterator =…
gontard
  • 27,180
  • 10
  • 90
  • 117
520
votes
18 answers

What exactly are iterator, iterable, and iteration?

What is the most basic definition of "iterable", "iterator" and "iteration" in Python? I have read multiple definitions but I am unable to identify the exact meaning as it still won't sink in. Can someone please help me with the 3 definitions in…
thechrishaddad
  • 6,066
  • 7
  • 24
  • 29
511
votes
10 answers

What is the most effective way to get the index of an iterator of an std::vector?

I'm iterating over a vector and need the index the iterator is currently pointing at. AFAIK this can be done in two ways: it - vec.begin() std::distance(vec.begin(), it) What are the pros and cons of these methods?
cairol
  • 8,083
  • 8
  • 26
  • 25
441
votes
15 answers

Get the first item from an iterable that matches a condition

I would like to get the first item from a list matching a condition. It's important that the resulting method not process the entire list, which could be quite large. For example, the following function is adequate: def first(the_iterable, condition…
Chris Phillips
  • 10,390
  • 3
  • 33
  • 45
387
votes
10 answers

How to avoid "ConcurrentModificationException" while removing elements from `ArrayList` while iterating it?

I'm trying to remove some elements from an ArrayList while iterating it like this: for (String str : myArrayList) { if (someCondition) { myArrayList.remove(str); } } Of course, I get a ConcurrentModificationException when trying to…
Ernestas Gruodis
  • 7,959
  • 14
  • 47
  • 110
352
votes
11 answers

Sorting a vector in descending order

Should I use std::sort(numbers.begin(), numbers.end(), std::greater()); or std::sort(numbers.rbegin(), numbers.rend()); // note: reverse iterators to sort a vector in descending order? Are there any benefits or drawbacks with one approach…
fredoverflow
  • 244,751
  • 91
  • 367
  • 645
346
votes
8 answers

How to implement an STL-style iterator and avoid common pitfalls?

I made a collection for which I want to provide an STL-style, random-access iterator. I was searching around for an example implementation of an iterator but I didn't find any. I know about the need for const overloads of [] and * operators. What…
Tamás Szelei
  • 21,950
  • 16
  • 99
  • 173
1
2 3
99 100