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.

13012 questions
11167
votes
38 answers

What does the "yield" keyword do?

What is the use of the yield keyword in Python, and 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.
  • 126,349
  • 16
  • 50
  • 61
1015
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
  • 108,984
  • 95
  • 246
  • 308
687
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
  • 13,250
  • 24
  • 66
  • 100
637
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,437
  • 5
  • 19
  • 13
617
votes
12 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
  • 6,309
  • 3
  • 15
  • 8
609
votes
10 answers

Build a basic Python iterator

How would one create an iterative function (or iterator object) in python?
akdom
  • 28,041
  • 24
  • 70
  • 79
579
votes
6 answers

Iterator invalidation rules

What are the iterator invalidation rules for C++ containers? Preferably in a summary list format. _(Note: This is meant to be an entry to [Stack Overflow's C++ FAQ](https://stackoverflow.com/questions/tagged/c++-faq). If you want to critique the…
Lightness Races in Orbit
  • 358,771
  • 68
  • 593
  • 989
505
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
  • 25,988
  • 10
  • 86
  • 116
488
votes
17 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
  • 5,726
  • 7
  • 24
  • 29
486
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
  • 7,723
  • 8
  • 25
  • 25
461
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…
nbroeking
  • 6,010
  • 3
  • 15
  • 38
383
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
  • 9,741
  • 3
  • 31
  • 43
371
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,563
  • 12
  • 45
  • 104
332
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,352
  • 16
  • 94
  • 169
331
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
  • 237,063
  • 85
  • 359
  • 638
1
2 3
99 100