TypeError: 'datetime.datetime' object is not iterable

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Check out my new book

TypeError: 'datetime.datetime' object is not iterable #

The Python "TypeError: 'datetime.datetime' object is not iterable" occurs when we try to iterate over a datetime object instead of an iterable (e.g. a list). To solve the error, track down where the variable got assigned a datetime object and correct the assignment.

typeerror datetime datetime object is not iterable

Here is an example of how the error occurs.

main.py
from datetime import datetime result = datetime.today() print(result) # 👉️ 2022-05-21 19:25:15.889971 # ⛔️ TypeError: 'datetime.datetime' object is not iterable for i in result: print(i)

We are trying to iterate over a datetime object, but datetime objects are not iterable.

You have to figure out how the value got assigned a datetime object and correct the assignment to an iterable such as a list, string, tuple, etc.

Make sure you aren't reassigning an iterable to a datetime object somewhere by mistake.

Another common cause of the error is passing a datetime object to the built-in constructors, e.g. list(), dict(), tuple() and set().

The following 4 calls to the built-in constructors cause the error.

main.py
from datetime import datetime result = datetime.today() # ⛔️ TypeError: 'datetime.datetime' object is not iterable list(result) dict(result) tuple(result) set(result)

To solve the error, we have to correct the assignment and figure out where the datetime object is coming from.

Here are working examples of using the 4 built-ins.

main.py
l = list(['a', 'b', 'c']) print(l) # 👉️ ['a', 'b', 'c'] d = dict(name='Alice', age=30) print(d) # 👉️ {'name': 'Alice', 'age': 30} t = tuple([1, 2, 3]) print(t) # 👉️ (1, 2, 3) s = set(['a', 'b', 'a']) print(s) # 👉️ {'a', 'b'}

You have to figure out where the datetime object came from and correct the assignment.

If you need to check if an object is iterable, use a try/except statement.

main.py
my_str = 'hello' try: my_iterator = iter(my_str) for i in my_iterator: print(i) # 👉️ h, e, l, l, o except TypeError as te: print(te)

The iter() function raises a TypeError if the passed in value doesn't support the __iter__() method or the sequence protocol (the __getitem__() method).

If we pass a non-iterable object like a datetime object to the iter() function, the except block is ran.

main.py
from datetime import datetime result = datetime.today() try: my_iterator = iter(result) for i in my_iterator: print(i) except TypeError as te: print(te) # 👉️ 'datetime.datetime' object is not iterable

Examples of iterables include all sequence types (list, str, tuple) and some non-sequence types like dict, file objects and other objects that define an __iter__() or a __getitem__() method.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.