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.

13876 questions
12259
votes
48 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.
  • 138,304
  • 19
  • 53
  • 61
1164
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
  • 118,166
  • 97
  • 255
  • 316
832
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,280
  • 28
  • 68
  • 101
727
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,409
  • 3
  • 16
  • 8
661
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,757
  • 5
  • 21
  • 13
649
votes
10 answers

How to build a basic iterator?

How would one create an iterative function (or iterator object) in python?
akdom
  • 30,801
  • 26
  • 73
  • 79
607
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,640
  • 4
  • 17
  • 38
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
  • 371,860
  • 74
  • 627
  • 1,028
579
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,940
  • 10
  • 92
  • 117
537
votes
17 answers

What are iterator, iterable, and iteration?

What are "iterable", "iterator", and "iteration" in Python? How are they defined?
thechrishaddad
  • 6,276
  • 7
  • 26
  • 31
527
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,323
  • 8
  • 26
  • 25
474
votes
16 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,739
  • 3
  • 33
  • 45
398
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,164
  • 4
  • 29
  • 49
392
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,259
  • 14
  • 50
  • 112
370
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
  • 249,710
  • 92
  • 373
  • 647
1
2 3
99 100