By user7692855


2018-02-13 13:16:49 8 Comments

I am looking to create a string of all errors returned from the API. There may be multiple errors returned in a list. Each error is a dict and the string I wish to access its the reason:

  result: {
       errors: [{
          error: 'invalid_input',
          code: 409,
          reason: 'the inputted date field does not match required format'

       },
       {
          error: 'invalid_input',
          code: 409,
          reason: 'the inputted message field does not match required format'
        }

  }

What I have tried is:

return_string = ""
if errors in result:
    for error in errors:
        returned_string += " {}".format(error['reason'])

Is there a more pythonic way to do it?

3 comments

@Thomas 2018-02-13 13:20:14

There are several typos in your code. But the more Pythonic way would be with a list comprehension generator expression:

return_string = ""
if "errors" in result:
    return_string = " ".join(error['reason'] for error in result['errors'])

Or even in one line:

return_string = " ".join(error['reason'] for error in result.get('errors', []))

@Chris_Rands 2018-02-13 13:24:41

you mean a generator expression, no list comprehensions here

@bro-grammer 2018-02-13 13:56:09

You can use operator.itemgetter along with map to fetch the reason key from your list of dicts like

>>> from operator import itemgetter
>>> error_list = list(map(itemgetter('reason'),r['errors']))

This will give you output like

>>> ['the inputted date field does not match required format', 'the inputted message field does not match required format']

Next you can use join to join these strings together as one error message

>>> "".join(error_list)
>>> 'the inputted date field does not match required formatthe inputted message field does not match required format'

You can also specify the character on which you want seperate these two string

>>> " ".join(error_list) #Whitespace
>>> 'the inputted date field does not match required format the inputted message field does not match required format'

If you prefer one-liners

>>> " ".join(map(itemgetter('reason'),r['errors']))

@Rakesh 2018-02-13 13:20:49

result = {
       "errors": [{
          "error": 'invalid_input',
          "code": 409,
          "reason": 'the inputted date field does not match required format'

       },
       {
          "error": 'invalid_input',
          "code": 409,
          "reason": 'the inputted message field does not match required format'
        }]

  }

return_string = ""
if result.get("errors", None):
    for error in result["errors"]:
        return_string += " {}\n".format(error['reason'])
print return_string

Output:

the inputted date field does not match required format
 the inputted message field does not match required format

Related Questions

Sponsored Content

7 Answered Questions

[SOLVED] How to lowercase a string in Python?

53 Answered Questions

[SOLVED] Calling an external command in Python

28 Answered Questions

[SOLVED] Finding the index of an item given a list containing it in Python

  • 2008-10-07 01:39:38
  • Eugene M
  • 2855714 View
  • 2394 Score
  • 28 Answer
  • Tags:   python list

25 Answered Questions

[SOLVED] How can I safely create a nested directory in Python?

40 Answered Questions

[SOLVED] How do I check whether a file exists without exceptions?

11 Answered Questions

[SOLVED] Is there a way to substring a string?

  • 2009-03-19 17:29:41
  • Joan Venge
  • 2231400 View
  • 1688 Score
  • 11 Answer
  • Tags:   python string

23 Answered Questions

15 Answered Questions

[SOLVED] What are metaclasses in Python?

28 Answered Questions

[SOLVED] How to concatenate two lists in Python?

  • 2009-11-12 07:04:09
  • y2k
  • 1558911 View
  • 1785 Score
  • 28 Answer
  • Tags:   python list

14 Answered Questions

[SOLVED] Does Python have a string 'contains' substring method?

Sponsored Content