'How to add datetime as a dict value?

In for loop I'm parsing datetime.datetime objects like 2016-06-29 00:00:00 out of the string values of my dictionary. In order to have date information separately from the rest of exident data I'm trying to add these objects as a key-value pair to existing dict.

      for i in enumerate(my_list):
          date = dateparser.parse(i[1].get('exident_data')[0:8])
          my_list[date] = date

... but I fail and getting:

TypeError: list indices must be integers or slices, not datetime.datetime

How to do it? Can't find the answer...



Solution 1:[1]

Two things: create a new list in output and use a string as key value for this list and not datetime.datetime

Solution 2:[2]

Try a different approach:

import datetime
today = datetime.datetime.now()
mydate = {}
mydate['day'] = today.day
mydate['month'] = today.month
mydate['year'] = today.year
mydate['hour'] = today.hour

Try datetime documentation

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 B. Hel
Solution 2 jalazbe