Questions tagged [iterable]

An iterable is an object, such as a string or collection, that can be iterated over, yielding up its members one at a time.

1303 questions
100
votes
4 answers

Why do I get "TypeError: 'int' object is not iterable" when trying to sum digits of a number?

Here's my code: import math print("Hey, lets solve Task 4 :)") number1 = input("How many digits do you want to look at? ") number2 = input("What would you like the digits to add up to? ") if number1 == 1: cow = range(0,10) elif number1 == 2: …
user2908001
99
votes
8 answers

How can I count the number of items in an arbitrary iterable (such as a generator)?

Suppose I have an arbitrary iterable - for example, a generator that iterates over lines of a file and yields the ones matching a regex. How can I count the number of items in that iterable, supposing that I don't care about the elements themselves?
Fred Foo
  • 355,277
  • 75
  • 744
  • 836
96
votes
16 answers

Using Objects in For Of Loops

Why isn't is possible to use objects in for of loops? Or is this a browser bug? This code doesn't work in Chrome 42, saying undefined is not a function: test = { first: "one"} for(var item of test) { console.log(item) }
Daniel Herr
  • 19,083
  • 6
  • 44
  • 61
86
votes
4 answers

Why does Java not allow foreach on iterators (only on iterables)?

Possible Duplicate: Why is Java's Iterator not an Iterable? Idiomatic way to use for-each loop given an iterator? Can we use for-each loop for iterating the objects of Iterator type? The foreach loop are as far as I know syntax sugar added in Java…
Has QUIT--Anony-Mousse
  • 76,138
  • 12
  • 138
  • 194
81
votes
4 answers

Opposite of any() function

The Python built-in function any(iterable) can help to quickly check if any bool(element) is True in a iterable type. >>> l = [None, False, 0] >>> any(l) False >>> l = [None, 1, 0] >>> any(l) True But is there an elegant way or function in Python…
Ekeyme Mo
  • 1,247
  • 1
  • 9
  • 11
78
votes
7 answers

Shortest way to get first item of `OrderedDict` in Python 3

What's the shortest way to get first item of OrderedDict in Python 3? My best: list(ordered_dict.items())[0] Quite long and ugly. I can think of: next(iter(ordered_dict.items())) # Fixed, thanks Ashwini But it's not very self-describing.…
Ram Rachum
  • 84,019
  • 84
  • 236
  • 374
72
votes
3 answers

Unittest's assertEqual and iterables - only check the contents

Is there a 'decent' way in unittest to check the equality of the contents of two iterable objects? I am using a lot of tuples, lists and numpy arrays and I usually only want to test for the contents and not for the type. Currently I am simply…
Lucas Hoepner
  • 1,437
  • 1
  • 16
  • 21
69
votes
6 answers

Why aren't Enumerations Iterable?

In Java 5 and above you have the foreach loop, which works magically on anything that implements Iterable: for (Object o : list) { doStuff(o); } However, Enumerable still does not implement Iterable, meaning that to iterate over an Enumeration…
SCdF
  • 57,260
  • 24
  • 77
  • 113
68
votes
1 answer

collections.Iterable vs typing.Iterable in type annotation and checking for Iterable

I found that in Python both collections.Iterable and typing.Iterable can be used in type annotation and checking for whether an object is iterable, i.e., both isinstance(obj, collections.Iterable) and isinstance(obj, typing.Iterable) works. My…
Benjamin Du
  • 1,391
  • 1
  • 17
  • 25
66
votes
5 answers

Collection to Iterable

How can I get a java.lang.Iterable from a collection like a Set or a List? Thanks!
myborobudur
  • 4,385
  • 8
  • 40
  • 61
63
votes
4 answers

Does Rust have an equivalent to Python's list comprehension syntax?

Python list comprehension is really simple: >>> l = [x for x in range(1, 10) if x % 2 == 0] >>> [2, 4, 6, 8] Does Rust have an equivalent syntax like: let vector = vec![x for x in (1..10) if x % 2 == 0] // [2, 4, 6, 8]
Darkaird
  • 2,700
  • 4
  • 15
  • 29
59
votes
1 answer

How come regex match objects aren't iterable even though they implement __getitem__?

As you may know, implementing a __getitem__ method makes a class iterable: class IterableDemo: def __getitem__(self, index): if index > 3: raise IndexError return index demo = IterableDemo() print(demo[2]) #…
Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
58
votes
8 answers

Why doesn't the String class in Java implement Iterable?

Many Java framework classes implement Iterable, however String does not. It makes sense to iterate over characters in a String, just as one can iterate over items in a regular array. Is there a reason why String does not implement Iterable?
user333335
  • 583
  • 1
  • 4
  • 4
57
votes
6 answers

Check if all values of iterable are zero

Is there a good, succinct/built-in way to see if all the values in an iterable are zeros? Right now I am using all() with a little list comprehension, but (to me) it seems like there should be a more expressive method. I'd view this as somewhat…
mjschultz
  • 1,936
  • 1
  • 18
  • 20
55
votes
2 answers

Chart of IEnumerable LINQ equivalents in Scala?

Possible Duplicate: LINQ analogues in Scala I am looking for chart which shows equivalents in Scala of LINQ methods for IEnumerable: First is head Select is map SingleOrDefault is ... (I don't know) ... and so on Does anyone know anything of…
greenoldman
  • 16,895
  • 26
  • 119
  • 185
1
2
3
86 87