iterable

Python – TypeError: 'int' object is not iterable

Why do I get "TypeError: 'int' object is not iterable" when trying to sum digits of a number? Question: 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? ") …

Total answers: 4

What are iterator, iterable, and iteration?

What are iterator, iterable, and iteration? Question: What are "iterable", "iterator", and "iteration" in Python? How are they defined? Asked By: thechrishaddad || Source Answers: An iterable is a object which has a __iter__() method. It can possibly iterated over several times, such as list()s and tuple()s. An iterator is the object which iterates. It …

Total answers: 16

Unittest's assertEqual and iterables – only check the contents

Unittest's assertEqual and iterables – only check the contents Question: 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 …

Total answers: 3

What's the shortest way to count the number of items in a generator/iterator?

What's the shortest way to count the number of items in a generator/iterator? Question: If I want the number of items in an iterable without caring about the elements themselves, what would be the pythonic way to get that? Right now, I would define def ilen(it): return sum(itertools.imap(lambda _: 1, it)) # or just map …

Total answers: 7

How do I add the contents of an iterable to a set?

How do I add the contents of an iterable to a set? Question: What is the “one […] obvious way” to add all items of an iterable to an existing set? Asked By: Ian Mackinnon || Source Answers: Use list comprehension. Short circuiting the creation of iterable using a list for example 🙂 >>> x …

Total answers: 6

Check if all values of iterable are zero

Check if all values of iterable are zero Question: 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 …

Total answers: 6

In Python, how do I determine if an object is iterable?

In Python, how do I determine if an object is iterable? Question: Is there a method like isiterable? The only solution I have found so far is to call hasattr(myObj, ‘__iter__’) But I am not sure how fool-proof this is. Asked By: willem || Source Answers: Checking for __iter__ works on sequence types, but it …

Total answers: 24

Length of generator output

Length of generator output Question: Python provides a nice method for getting length of an eager iterable, len(x) that is. But I couldn’t find anything similar for lazy iterables represented by generator comprehensions and functions. Of course, it is not hard to write something like: def iterlen(x): n = 0 try: while True: next(x) n …

Total answers: 9