MathMan 99
MathMan 99

Reputation: 755

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

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.

Upvotes: 0

Views: 489

Answers (1)

Corralien
Corralien

Reputation: 120409

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']

Upvotes: 2

Related Questions