shivam patel
shivam patel

Reputation: 75

how to get string value from a list in python

result = [{'start_date': datetime.date(2019, 1, 20)}, {'start_date': datetime.date(2019, 1, 21)}]

I want to get output like in list format:

result = ['2019-1-20', '2019-1-21']

Upvotes: 1

Views: 6757

Answers (3)

fixatd
fixatd

Reputation: 1404

I would use strftime to format your dates to what your desired form:

import datetime

result = [{'start_date': datetime.date(2019, 1, 20)}, {'start_date': datetime.date(2019, 1, 21)}]

for date_val in result:
    print date_val['start_date'].strftime('%Y-%m-%d')

# Results
2019-01-20
2019-01-21

If you want just the first element:

print result[0]['start_date'].strftime('%Y-%m-%d')

See: strftime documentation

Upvotes: 0

Anonymous
Anonymous

Reputation: 699

Use Below code:

[print("Result :",Dict["start_date"]) for Dict in result]

Output:

Result : 2019-01-20
Result : 2019-01-21

Upvotes: 0

Taohidul Islam
Taohidul Islam

Reputation: 5424

Cast type to str :

result = str(result[0]['start_date'])

or

result = str(result[0].get('start_date']))

Upvotes: 1

Related Questions