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 method
  • Reverse iterators typically have a previous method
  • Iterators sometimes 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.

14461 questions
12706
votes
50 answers

What does the "yield" keyword do in Python?

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.
  • 143,260
  • 19
  • 55
  • 62
1239
votes
8 answers

How do I iterate through two lists in parallel?

I have two iterables, and I want to go over them in pairs: foo = [1, 2, 3] bar = [4, 5, 6] for (f, b) in iterate_together(foo, bar): print("f:", f, " | b:", b) That should result in: f: 1 | b: 4 f: 2 | b: 5 f: 3 | b: 6 One way to do it…
Nathan Fellman
  • 122,701
  • 101
  • 260
  • 319
882
votes
11 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
  • 15,968
  • 28
  • 69
  • 107
775
votes
15 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,905
  • 3
  • 17
  • 8
685
votes
10 answers

How to build a basic iterator?

How can I create an iterator in Python? For example, suppose I have a class whose instances logically "contain" some values: class Example: def __init__(self, values): self.values = values I want to be able to write code like: e =…
akdom
  • 32,264
  • 27
  • 73
  • 79
671
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
  • 8,360
  • 5
  • 20
  • 40
671
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,897
  • 5
  • 22
  • 13
608
votes
11 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
  • 28,720
  • 11
  • 94
  • 117
595
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
  • 378,754
  • 76
  • 643
  • 1,055
561
votes
15 answers

What are iterator, iterable, and iteration?

What are "iterable", "iterator", and "iteration" in Python? How are they defined? See also: How to build a basic iterator?
thechrishaddad
  • 6,523
  • 7
  • 27
  • 33
553
votes
18 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
  • 11,607
  • 3
  • 34
  • 45
539
votes
9 answers

How do I 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. What are the pros and cons of the following methods? it - vec.begin() std::distance(vec.begin(), it)
cairol
  • 8,573
  • 9
  • 27
  • 25
454
votes
8 answers

C++ Loop through Map

I want to iterate through each element in the map without knowing any of its string-int values or keys. What I have so far: void output(map table) { map::iterator it; for (it = table.begin(); it…
NoName
  • 9,824
  • 5
  • 32
  • 52
398
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
  • 8,567
  • 14
  • 55
  • 117
386
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
  • 256,549
  • 94
  • 388
  • 662
1
2 3
99 100