Accessing the index in ‘for’ loops?

Using an additional state variable, such as an index variable (which you would normally use in languages such as C or PHP), is considered non-pythonic. The better option is to use the built-in function enumerate(), available in both Python 2 and 3:

Loop through an array in JavaScript

Three main options: for (var i = 0; i < xs.length; i++) { console.log(xs[i]); } xs.forEach((x, i) => console.log(x)); for (const x of xs) { console.log(x); } Detailed examples are below. 1. Sequential for loop:  Run code snippet Pros Works on every environment You can use break and continue flow control statements Cons Too verbose Imperative Easy to have off-by-one errors (sometimes also … Read more

Accessing the index in ‘for’ loops?

Using an additional state variable, such as an index variable (which you would normally use in languages such as C or PHP), is considered non-pythonic. The better option is to use the built-in function enumerate(), available in both Python 2 and 3:

Loop through an array in JavaScript

Yes, assuming your implementation includes the for…of feature introduced in ECMAScript 2015 (the “Harmony” release)… which is a pretty safe assumption these days. It works like this: Or better yet, since ECMAScript 2015 also provides block-scoped variables: (The variable s is different on each iteration, but can still be declared const inside the loop body as long as it isn’t modified there.) A … Read more

Why there is no do while loop in python

There is no do…while loop because there is no nice way to define one that fits in the statement: indented block pattern used by every other Python compound statement. As such proposals to add such syntax have never reached agreement. Nor is there really any need to have such a construct, not when you can just do: and have the exact … Read more

For-each over an array in JavaScript

TL;DR Your best bets are usually a for-of loop (ES2015+ only; spec | MDN) – simple and async-friendly forEach (ES5+ only; spec | MDN) (or its relatives some and such) – not async-friendly (but see details) a simple old-fashioned for loop – async-friendly (rarely) for-in with safeguards – async-friendly Some quick “don’t”s: Don’t use for-in … Read more