Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: 'datetime.date' object is not iterable while iterating over dict by date

I am trying to iterate over dict, which contains dates as keys:

for d, dir in subdirs:
    print("d=", d, ", dir=", dir)

where subdirs is

datetime.date(2016, 9, 29): ['tiles/37/U/DB/2016/9/29/0/'],datetime.date(2017, 2, 23): ['tiles/37/U/DB/2017/2/23/0/'], datetime.date(2016, 4, 5): ['tiles/37/U/DB/2016/4/5/0/']

and so on.

I am receiving

TypeError: 'datetime.date' object is not iterable

Why and how to fix?

like image 974
Dims Avatar asked Jun 25 '26 17:06

Dims


1 Answers

Try this -

for d, dir in subdirs.items():
    print("d=", d, ", dir=", dir)

If subdirs is a dict, using it in for will give you only the keys(in your case datetime.date object) and d, dir will try to unpack them. That is why the error.

Using the items() will return the (key, value) pair which will be unpacked and stored in d and dirs respectively

like image 121
kuro Avatar answered Jun 27 '26 07:06

kuro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!