An elegant and fast way to consecutively iterate over two or more containers in Python?

Depending on what order you want to process the items: import itertools for items in itertools.izip(deque1, deque2, deque3): for item in items: some_action(item) for item in itertools.chain(deque1, deque2, deque3): some_action(item) I’d recommend doing this to avoid hard-coding the actual deques or number of deques: deques = [deque1, deque2, deque3] for item in itertools.chain(*deques): some_action(item) To … Read more

What characters are grouped with Array.from?

Array.from first tries to invoke the iterator of the argument if it has one, and strings do have iterators, so it invokes String.prototype[Symbol.iterator], so let’s look up how the prototype method works. It’s described in the specification here: Let O be ? RequireObjectCoercible(this value). Let S be ? ToString(O). Return CreateStringIterator(S). Looking up CreateStringIterator eventually … Read more

Detect last iteration in FOR OF loop in ES6 javascript

One approach is using Array.prototype.entries(): for (const [i, value] of arr.entries()) { if (i === arr.length – 1) { // do your thing } } Another way is keeping a count outside the loop like Shidersz suggested. I don’t think you want to check indexOf(item) though because that poses a problem if the last item … Read more