Star * operator on left vs right side of an assignment statement

The difference between these two cases are explained when also taking into consideration the initial PEP for extended unpacking: PEP 3132 — Extended iterable unpacking. In the Abstract for that PEP we can see that: This PEP proposes a change to iterable unpacking syntax, allowing to specify a “catch-all” name which will be assigned a … Read more

What is the technical definition of a Javascript iterable and how do you test for it?

What is the real definition of a Javascript iterable using the term as the ES6 specification does? §25.1.1.1 defines “The Iterable Interface”. They’re objects with a Symbol.iterator-keyed method that returns a valid Iterator (which in turn is an object expected to behave as it should according to §25.1.1.2). How do you test for it in … Read more

Java: why can’t iterate over an iterator?

Most likely the reason for this is because iterators are not reusable; you need to get a fresh Iterator from the Iterable collection each time you want to iterate over the elements. However, as a quick fix: private static <T> Iterable<T> iterable(final Iterator<T> it){ return new Iterable<T>(){ public Iterator<T> iterator(){ return it; } }; } … Read more

Why do I get “TypeError: ‘int’ object is not iterable” when trying to sum digits of a number? [duplicate]

Your problem is with this line: number4 = list(cow[n]) It tries to take cow[n], which returns an integer, and make it a list. This doesn’t work, as demonstrated below: >>> a = 1 >>> list(a) Traceback (most recent call last): File “<stdin>”, line 1, in <module> TypeError: ‘int’ object is not iterable >>> Perhaps you … Read more

Java: Get first item from a collection

Looks like that is the best way to do it: String first = strs.iterator().next(); Great question… At first, it seems like an oversight for the Collection interface. Note that “first” won’t always return the first thing you put in the collection, and may only make sense for ordered collections. Maybe that is why there isn’t … Read more