Home > Mobile >  Python: convert list of dictionary keys containing timestamps into a list of strings
Python: convert list of dictionary keys containing timestamps into a list of strings

Time:11-07

I have the following list:

[(Timestamp('2017-01-26 00:00:00'), -0.19),
 (Timestamp('2018-04-05 10:00:00'), -0.15)]

I want to change the keys from timestamps into strings and get the following list

['2017-01-26 00:00:00',
 '2018-04-05 10:00:00']

Can this be done with a single line of code?

Thank you.

CodePudding user response:

Use a comprehension:

lst = [(Timestamp('2017-01-26 00:00:00'), -0.19),
       (Timestamp('2018-04-05 10:00:00'), -0.15)]

dt = [str(t[0]) for t in lst]
print(dt)

# Output
['2017-01-26 00:00:00', '2018-04-05 10:00:00']
  • Related