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.
iteration
915
questions
931
votes
25
answers
934k
views
How to remove items from a list while iterating?
I'm iterating over a list of tuples in Python, and am attempting to remove them if they meet certain criteria.
for tup in somelist:
if determine(tup):
code_to_remove_tup
What should I ...
2080
votes
28
answers
1.1m
views
Why is using "for...in" for array iteration a bad idea?
I've been told not to use for...in with arrays in JavaScript. Why not?
5722
votes
41
answers
5.4m
views
Loop (for each) over an array in JavaScript
How can I loop through all the entries in an array using JavaScript?
1301
votes
31
answers
524k
views
Iterating through a Collection, avoiding ConcurrentModificationException when removing objects in a loop
We all know you can't do the following because of ConcurrentModificationException:
for (Object i : l) {
if (condition(i)) {
l.remove(i);
}
}
But this apparently works sometimes, but ...
9
votes
3
answers
4k
views
How can I collect the results of a repeated calculation in a list, dictionary etc. (or make a copy of a list with each element modified)?
There are a great many existing Q&A on Stack Overflow on this general theme, but they are all either poor quality (typically, implied from a beginner's debugging problem) or miss the mark in some ...
3989
votes
46
answers
3.4m
views
How do I efficiently iterate over each entry in a Java Map?
If I have an object implementing the Map interface in Java and I wish to iterate over every pair contained within it, what is the most efficient way of going through the map?
Will the ordering of ...
570
votes
15
answers
411k
views
What are iterator, iterable, and iteration?
What are "iterable", "iterator", and "iteration" in Python? How are they defined?
See also: How to build a basic iterator?
154
votes
8
answers
133k
views
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)?
...
2282
votes
7
answers
459k
views
How does PHP 'foreach' actually work?
Let me prefix this by saying that I know what foreach is, does and how to use it. This question concerns how it works under the bonnet, and I don't want any answers along the lines of "this is how you ...
453
votes
21
answers
201k
views
Way to go from recursion to iteration
I've used recursion quite a lot during my many years of programming to solve simple problems, but I'm fully aware that sometimes you need iteration due to memory/speed problems.
So, sometime in the ...
657
votes
19
answers
824k
views
How to iterate over a JavaScript object?
I have an object in JavaScript:
{
abc: '...',
bca: '...',
zzz: '...',
xxx: '...',
ccc: '...',
// ...
}
I want to use a for loop to get its properties. And I want to iterate ...
3771
votes
7
answers
4.5m
views
Iterate through a HashMap [duplicate]
What's the best way to iterate over the items in a HashMap?
164
votes
3
answers
40k
views
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 ...
164
votes
23
answers
144k
views
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<std::tuple_size<T...>::value; ++i)
std::get<i>(my_tuple).do_sth();
but this doesn't work:
...
721
votes
13
answers
1.3m
views
Ways to iterate over a list in Java
Being somewhat new to the Java language I'm trying to familiarize myself with all the ways (or at least the non-pathological ones) that one might iterate through a list (or perhaps other collections) ...