Questions tagged [iteration]

Iterations are the successive repetitions in loops such as for, foreach or while. Questions with this tag are often concerned about how to best handle a collection of data.

Wiki

Definition: Iteration is the repetition of a block of statements within a computer program.

In most computer programming languages, an iteration is a process that allows code to be executed repeatedly based on a given Boolean condition. Loops can be thought of as an iteration statements.

Examples

  • for
  • foreach
  • while
  • do while

Tag usage

The tag can be used for all programming related problems in implementing iterative approach to call a programming code repeatedly. The tag can be used in problems in implementing iteration using programming constructs and loops. Tag can also be used with other tags , , and .

Read more

10795 questions
116
votes
17 answers

Loop that also accesses previous and next values

How can I iterate over a list of objects, accessing the previous, current, and next items? Like this C/C++ code, in Python? foo = somevalue; previous = next = 0; for (i=1; i
dir01
  • 2,120
  • 4
  • 18
  • 17
113
votes
9 answers

Start index for iterating Python list

What is the best way to set a start index when iterating a list in Python. For example, I have a list of the days of the week - Sunday, Monday, Tuesday, ... Saturday - but I want to iterate through the list starting at Monday. What is the best…
Vincent Catalano
  • 2,259
  • 4
  • 22
  • 29
111
votes
6 answers

Best way to iterate through a Perl array

Which is the best implementation(in terms of speed and memory usage) for iterating through a Perl array? Is there any better way? (@Array need not be retained). Implementation 1 foreach (@Array) { SubRoutine($_); } Implementation…
Jean
  • 21,665
  • 24
  • 69
  • 119
105
votes
14 answers

What is the cleanest way to get the sum of numbers in a collection/list in Dart?

I don't like using an indexed array for no reason other than I think it looks ugly. Is there a clean way to sum with an anonymous function? Is it possible to do it without using any outside variables?
Phlox Midas
  • 4,093
  • 4
  • 35
  • 56
104
votes
10 answers

std::queue iteration

I need to iterate over std::queue. www.cplusplus.com says: By default, if no container class is specified for a particular queue class, the standard container class template deque is used. So can I somehow get to the queue's underlying deque and…
jackhab
  • 17,128
  • 37
  • 99
  • 136
102
votes
15 answers

Iterate through Nested JavaScript Objects

I'm trying to iterate through a nested object to retrieve a specific object identified by a string. In the sample object below, the identifier string is the "label" property. I can't wrap my head around how to iterate down through the tree to return…
NewToThis
  • 3,197
  • 5
  • 22
  • 10
102
votes
10 answers

Intersecting two dictionaries

I am working on a search program over an inverted index. The index itself is a dictionary whose keys are terms and whose values are themselves dictionaries of short documents, with ID numbers as keys and their text content as values. To perform an…
norman
  • 5,128
  • 13
  • 44
  • 75
98
votes
3 answers

How to reverse tuples in Python?

Is this possible? Doesn't have to be in place, just looking for a way to reverse a tuple so I can iterate on it backwards.
Joan Venge
  • 315,713
  • 212
  • 479
  • 689
96
votes
14 answers

"For" loop first iteration

I would like to inquire if there is an elegant pythonic way of executing some function on the first loop iteration. The only possibility I can think of is: first = True for member in something.get(): if first: root.copy(member) …
Rince
  • 2,195
  • 3
  • 21
  • 35
94
votes
12 answers

Efficient iteration with index in Scala

Since Scala does not have old Java style for loops with index, // does not work val xs = Array("first", "second", "third") for (i=0; i
snappy
  • 2,761
  • 5
  • 23
  • 24
93
votes
4 answers

How do I modify an array while I am iterating over it in Ruby?

I'm just learning Ruby so apologies if this is too newbie for around here, but I can't work this out from the pickaxe book (probably just not reading carefully enough). Anyway, if I have an array like so: arr = [1,2,3,4,5] ...and I want to, say,…
brad
  • 9,573
  • 12
  • 62
  • 89
90
votes
12 answers

is the Java HashMap keySet() iteration order consistent?

I understand that the Set returned from a Map's keySet() method does not guarantee any particular order. My question is, does it guarantee the same order over multiple iterations. For example Map map = getMap(); for( K k : map.keySet()…
karoberts
  • 9,828
  • 4
  • 39
  • 39
87
votes
8 answers

How do I iterate through each element in an n-dimensional matrix in MATLAB?

I have a problem. I need to iterate through every element in an n-dimensional matrix in MATLAB. The problem is, I don't know how to do this for an arbitrary number of dimensions. I know I can say for i = 1:size(m,1) for j = 1:size(m,2) …
rlbond
  • 65,341
  • 56
  • 178
  • 228
87
votes
7 answers

Iterate over two arrays simultaneously

I am new to Swift. I have been doing Java programming. I have a scenario to code for in Swift. The following code is in Java. I need to code in Swift for the following scenario // With String array - strArr1 String strArr1[] =…
Lohith
  • 891
  • 1
  • 6
  • 5
86
votes
10 answers

Ruby each_with_index offset

Can I define the offset of the index in the each_with_index loop iterator? My straight forward attempt failed: some_array.each_with_index{|item, index = 1| some_func(item, index) } Edit: Clarification: I don't want an array offset I want that the…
Mark
  • 7,891
  • 5
  • 27
  • 36