1
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']
2
  • result = result[0]['start_date'] Commented Feb 4, 2019 at 8:02
  • Do you always want the first element or wait. do you want the result to be the first element of the list? Commented Feb 4, 2019 at 8:04

3 Answers 3

1

Cast type to str :

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

or

result = str(result[0].get('start_date']))
Sign up to request clarification or add additional context in comments.

Comments

0

Use Below code:

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

Output:

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.