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
152
votes
13 answers

Is if(items != null) superfluous before foreach(T item in items)?

I often come across code like the following: if ( items != null) { foreach(T item in items) { //... } } Basically, the if condition ensures that foreach block will execute only if items is not null. I'm wondering if the if…
Nawaz
  • 353,942
  • 115
  • 666
  • 851
149
votes
7 answers

JavaScript Set vs. Array performance

It may be because Set is relatively new to JavaScript but I haven't been able to find an article, on Stack Overflow or anywhere else, that talks about the performance difference between the two in JavaScript. So, what is the difference, in terms of…
snowfrogdev
  • 5,963
  • 3
  • 31
  • 58
146
votes
7 answers

Python using enumerate inside list comprehension

Lets suppose I have a list like this: mylist = ["a","b","c","d"] To get the values printed along with their index I can use Python's enumerate function like this >>> for i,j in enumerate(mylist): ... print i,j ... 0 a 1 b 2 c 3 d >>> Now, when…
RanRag
  • 48,359
  • 38
  • 114
  • 167
144
votes
8 answers

Does pandas iterrows have performance issues?

I have noticed very poor performance when using iterrows from pandas. Is it specific to iterrows and should this function be avoided for data of a certain size (I'm working with 2-3 million rows)? This discussion on GitHub led me to believe it is…
KieranPC
  • 8,525
  • 7
  • 22
  • 25
136
votes
13 answers

C# Iterating through an enum? (Indexing a System.Array)

I have the following code: // Obtain the string names of all the elements within myEnum String[] names = Enum.GetNames( typeof( myEnum ) ); // Obtain the values of all the elements within myEnum Array values = Enum.GetValues( typeof( myEnum )…
TK.
  • 46,577
  • 46
  • 119
  • 147
136
votes
7 answers

Remove Elements from a HashSet while Iterating

So, if I try to remove elements from a Java HashSet while iterating, I get a ConcurrentModificationException. What is the best way to remove a subset of the elements from a HashSet as in the following example? Set set = new…
Scott
134
votes
5 answers

How to iterate through a list of dictionaries in Jinja template?

I tried: list1 = [{"username": "abhi", "pass": 2087}] return render_template("file_output.html", list1=list1) In the template: {% for dictionary in list1…
user3089927
  • 3,575
  • 8
  • 25
  • 33
130
votes
10 answers

recursion versus iteration

Is it correct to say that everywhere recursion is used a for loop could be used? And if recursion is usually slower what is the technical reason for ever using it over for loop iteration? And if it is always possible to convert an recursion into a…
Breako Breako
  • 6,353
  • 14
  • 40
  • 59
128
votes
8 answers

How do I access properties of a javascript object if I don't know the names?

Say you have a javascript object like this: var data = { foo: 'bar', baz: 'quux' }; You can access the properties by the property name: var foo = data.foo; var baz = data["baz"]; But is it possible to get these values if you don't know the name of…
Adam Lassek
  • 35,156
  • 14
  • 91
  • 107
123
votes
13 answers

Is there a reason that we cannot iterate on "reverse Range" in ruby?

I tried to iterate backwards with using a Range and each: (4..0).each do |i| puts i end ==> 4..0 Iteration through 0..4 writes the numbers. On the other Range r = 4..0 seems to be ok, r.first == 4, r.last == 0. It seems to be strange to me that…
fifigyuri
  • 5,771
  • 8
  • 30
  • 50
122
votes
11 answers

PHP: Can I get the index in an array_map function?

I'm using a map in php like so: function func($v) { return $v * 2; } $values = array(4, 6, 3); $mapped = array_map(func, $values); var_dump($mapped); Is it possible to get the index of the value in the function? Also - if I'm writing code that…
Ollie Glass
  • 19,455
  • 21
  • 76
  • 107
122
votes
9 answers

What's the safest way to iterate through the keys of a Perl hash?

If I have a Perl hash with a bunch of (key, value) pairs, what is the preferred method of iterating through all the keys? I have heard that using each may in some way have unintended side effects. So, is that true, and is one of the two following…
Rudd Zwolinski
  • 26,712
  • 17
  • 57
  • 60
119
votes
11 answers

Time complexity of Euclid's Algorithm

I am having difficulty deciding what the time complexity of Euclid's greatest common denominator algorithm is. This algorithm in pseudo-code is: function gcd(a, b) while b ≠ 0 t := b b := a mod b a := t return a It…
Donald T
  • 10,234
  • 17
  • 63
  • 91
119
votes
5 answers

How to iterate std::set?

I have this code: std::set::iterator it; for (it = SERVER_IPS.begin(); it != SERVER_IPS.end(); ++it) { u_long f = it; // error here } There is no ->first value. How I can obtain the value?
Roman
  • 1,377
  • 2
  • 11
  • 12
119
votes
4 answers

Iterating a JavaScript object's properties using jQuery

Is there a jQuery way to perform iteration over an object's members, such as in: for (var member in obj) { ... } I just don't like this for sticking out from amongst my lovely jQuery notation!
tags2k
  • 82,117
  • 31
  • 79
  • 106
Key Value