Is there a way to handle key not found when using operator.itemgetter without using a try-except block?

Viewed 57

Is there any other option aside from wrapping itemgetter in a try/except block in case of a missing key?

Like dict.get('bar', 'foo')?

Example of usage:

currentUser = {
    "id": 24,
    "name": "John Doe",
    "website": "http://mywebsite.com",
    "description": "I am an actor",
    "email": "example@example.com",
    "phone_number": "+12345678",
    "username": "johndoe",
    "birth_date": "1991-02-23",
    "followers": 46263,
    "following": 345,
    "like": 204,
    "comments": 9
}

id, like, hhh = itemgetter("id","like", "hhh")(currentUser)
0 Answers
Related