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
227
votes
9 answers

"for" vs "each" in Ruby

I just had a quick question regarding loops in Ruby. Is there a difference between these two ways of iterating through a collection? # way 1 @collection.each do |item| # do whatever end # way 2 for item in @collection # do whatever end Just…
mportiz08
  • 10,206
  • 12
  • 40
  • 42
219
votes
3 answers

Is there a way to iterate over a dictionary?

I know NSDictionaries as something where you need a key in order to get a value. But how can I iterate over all keys and values in a NSDictionary, so that I know what keys there are, and what values there are? I know there is something called a…
HelloMoon
204
votes
6 answers

How to produce a range with step n in bash? (generate a sequence of numbers with increments)

The way to iterate over a range in bash is for i in {0..10}; do echo $i; done What would be the syntax for iterating over the sequence with a step? Say, I would like to get only even number in the above example.
SilentGhost
  • 307,395
  • 66
  • 306
  • 293
194
votes
5 answers

Is the order guaranteed for the return of keys and values from a LinkedHashMap object?

I know LinkedHashMap has a predictable iteration order (insertion order). Does the Set returned by LinkedHashMap.keySet() and the Collection returned by LinkedHashMap.values() also maintain this order?
user256239
  • 17,717
  • 26
  • 77
  • 89
186
votes
9 answers

Fastest way to iterate over all the chars in a String

In Java, what would the fastest way to iterate over all the chars in a String, this: String str = "a really, really long string"; for (int i = 0, n = str.length(); i < n; i++) { char c = str.charAt(i); } Or this: char[] chars =…
Óscar López
  • 232,561
  • 37
  • 312
  • 386
186
votes
6 answers

Python list iterator behavior and next(iterator)

Consider: >>> lst = iter([1,2,3]) >>> next(lst) 1 >>> next(lst) 2 So, advancing the iterator is, as expected, handled by mutating that same object. This being the case, I would expect: a = iter(list(range(10))) for i in a: print(i) …
lvc
  • 34,233
  • 10
  • 73
  • 98
183
votes
12 answers

Loop through a date range with JavaScript

Given two Date() objects, where one is less than the other, how do I loop every day between the dates? for(loopDate = startDate; loopDate < endDate; loopDate += 1) { } Would this sort of loop work? But how can I add one day to the loop…
Tom Gullen
  • 61,249
  • 84
  • 283
  • 456
182
votes
15 answers

How to iterate through range of Dates in Java?

In my script I need to perform a set of actions through range of dates, given a start and end date. Please provide me guidance to achieve this using Java. for ( currentDate = starDate; currentDate < endDate; currentDate++) { } I know the above…
user531743
161
votes
3 answers

How to loop through key/value object in Javascript?

var user = {}; now I want to create a setUsers() method that takes a key/value pair object and initializes the user variable. setUsers = function(data) { // loop and init user } where data is like: 234: "john", 23421: "smith", ....
Blankman
  • 259,732
  • 324
  • 769
  • 1,199
161
votes
7 answers

Is there a better way to iterate over two lists, getting one element from each list for each iteration?

I have a list of Latitudes and one of Longitudes and need to iterate over the latitude and longitude pairs. Is it better to: A. Assume that the lists are of equal lengths: for i in range(len(Latitudes)): …
chongman
  • 2,447
  • 4
  • 21
  • 23
158
votes
2 answers

Are for-loops in pandas really bad? When should I care?

Are for loops really "bad"? If not, in what situation(s) would they be better than using a more conventional "vectorized" approach?1 I am familiar with the concept of "vectorization", and how pandas employs vectorized techniques to speed up…
cs95
  • 379,657
  • 97
  • 704
  • 746
157
votes
8 answers

What is the perfect counterpart in Python for "while not EOF"

To read some text file, in C or Pascal, I always use the following snippets to read the data until EOF: while not eof do begin readline(a); do_something; end; Thus, I wonder how can I do this simple and fast in Python?
Allen Koo
  • 1,996
  • 3
  • 14
  • 15
154
votes
7 answers

Is there a way in Pandas to use previous row value in dataframe.apply when previous value is also calculated in the apply?

I have the following dataframe: Index_Date A B C D ================================ 2015-01-31 10 10 Nan 10 2015-02-01 2 3 Nan 22 2015-02-02 10 60 Nan 280 2015-02-03 10 100 Nan 250 Require: Index_Date…
ctrl-alt-delete
  • 3,696
  • 2
  • 24
  • 37
154
votes
24 answers

How can you iterate over the elements of an std::tuple?

How can I iterate over a tuple (using C++11)? I tried the following: for(int i=0; i::value; ++i) std::get(my_tuple).do_sth(); but this doesn't work: Error 1: sorry, unimplemented: cannot expand ‘Listener ...’ into a…
1521237
153
votes
10 answers

In Rust, is there a way to iterate through the values of an enum?

I come from a Java background and I might have something like enum Direction { NORTH, SOUTH, EAST, WEST} and I could do something with each of the values in turn with the enhanced for loop like: for(Direction dir : Direction.values()) { //do…
dougli1sqrd
  • 1,653
  • 2
  • 10
  • 8