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

24 Answered Questions

[SOLVED] What is the difference between @staticmethod and @classmethod in Python?

22 Answered Questions

17 Answered Questions

[SOLVED] Accessing the index in 'for' loops?

  • 2009-02-06 22:47:54
  • Joan Venge
  • 1274307 View
  • 2438 Score
  • 17 Answer
  • Tags:   python loops list

50 Answered Questions

[SOLVED] Calling an external command in Python

13 Answered Questions

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

39 Answered Questions

[SOLVED] How to check whether a file exists?

  • 2008-09-17 12:55:00
  • spence91
  • 2762264 View
  • 4190 Score
  • 39 Answer
  • Tags:   python file

10 Answered Questions

24 Answered Questions

[SOLVED] How to concatenate two lists in Python?

  • 2009-11-12 07:04:09
  • y2k
  • 1311434 View
  • 1573 Score
  • 24 Answer
  • Tags:   python list

14 Answered Questions

[SOLVED] What are metaclasses in Python?

10 Answered Questions

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

  • 2009-03-19 17:29:41
  • Joan Venge
  • 1947603 View
  • 1531 Score
  • 10 Answer
  • Tags:   python string

Sponsored Content