Skip to main content

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.

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?
Dante1986's user avatar
  • 59.4k
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 ...
iMack's user avatar
  • 38.9k
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?
burntsugar's user avatar
  • 57.8k
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 ...
DaveRandom's user avatar
  • 88.4k
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?
lYriCAlsSH's user avatar
  • 57.8k
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 ...
Claudiu's user avatar
  • 227k
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 ...
lfaraone's user avatar
  • 50.4k
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) ...
jacobq's user avatar
  • 11.4k
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 ...
nkuhta's user avatar
  • 10.9k
586 votes
9 answers
837k views

Iterating each character in a string using Python

How can I iterate over a string in Python (get each character from the string, one at a time, each time through a loop)?
Paradius's user avatar
  • 8,119
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?
thechrishaddad's user avatar
558 votes
28 answers
577k views

Iterating through a range of dates in Python

I have the following code to do this, but how can I do it better? Right now I think it's better than nested loops, but it starts to get Perl-one-linerish when you have a generator in a list ...
ShawnMilo's user avatar
  • 6,105
485 votes
17 answers
707k views

What is the easiest/best/most correct way to iterate through the characters of a string in Java?

Some ways to iterate through the characters of a string in Java are: Using StringTokenizer? Converting the String to a char[] and iterating over that. What is the easiest/best/most correct way to ...
Paul Wicks's user avatar
  • 64.8k
469 votes
5 answers
278k views

How to skip to next iteration in jQuery.each() util?

I'm trying to iterate through an array of elements. jQuery's documentation says: jquery.Each() documentation Returning non-false is the same as a continue statement in a for loop, it will skip ...
Josh's user avatar
  • 5,331
468 votes
8 answers
374k views

How do I iterate over an NSArray?

I'm looking for the standard idiom to iterate over an NSArray. My code needs to be suitable for OS X 10.4+.
Steve McLeod's user avatar
  • 52.2k

15 30 50 per page
1
2 3 4 5
728