Python – TypeError: ‘int’ object is not iterable

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

Get size of an Iterable in Java

TL;DR: Use the utility method Iterables.size(Iterable) of the great Guava library. Of your two code snippets, you should use the first one, because the second one will remove all elements from values, so it is empty afterwards. Changing a data structure for a simple query like its size is very unexpected. For performance, this depends … Read more

Kotlin’s Iterable and Sequence look exactly same. Why are two types required?

The key difference lies in the semantics and the implementation of the stdlib extension functions for Iterable<T> and Sequence<T>. For Sequence<T>, the extension functions perform lazily where possible, similarly to Java Streams intermediate operations. For example, Sequence<T>.map { … } returns another Sequence<R> and does not actually process the items until a terminal operation like … Read more

Convert Java Array to Iterable

Integer foo[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 }; List<Integer> list = Arrays.asList(foo); // or Iterable<Integer> iterable = Arrays.asList(foo); Though you need to use an Integer array (not an int array) for this to work. For primitives, you can use guava: Iterable<Integer> fooBar = Ints.asList(foo); <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>15.0</version> … Read more

Why is Java’s Iterator not an Iterable?

An iterator is stateful. The idea is that if you call Iterable.iterator() twice you’ll get independent iterators – for most iterables, anyway. That clearly wouldn’t be the case in your scenario. For example, I can usually write: public void iterateOver(Iterable<String> strings) { for (String x : strings) { System.out.println(x); } for (String x : strings) … Read more