Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8062211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:35:31+00:00 2026-06-05T10:35:31+00:00

I have this long list of try except statement: try: uri = entry_obj[‘media$group’][‘media$content’][0][‘url’] except

  • 0

I have this long list of try except statement:

    try:
       uri = entry_obj['media$group']['media$content'][0]['url']
    except (KeyError, IndexError):
       uri = None
    try:
       position = entry_obj['yt$position']['$t']
    except KeyError:
       position = None
    try:
       description = entry_obj['content']['$t']
    except KeyError:
       description = None
    try:
       seconds = entry_obj['media$group']['yt$duration']['seconds']
    except KeyError:
       seconds = None
    try:
       thumbnails = entry_obj['media$group']['media$thumbnail']
    except KeyError:
       thumbnails = None

Is there a more concise way to write this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T10:35:32+00:00Added an answer on June 5, 2026 at 10:35 am

    If you tire of figuring out what to use for default values in get() calls, just write a helper function:

    def resolve(root, *keys):
       for key in keys:
           try:
               root = root[key]
           except (KeyError, IndexError):
               return None
       return root
    

    Then you just write, e.g.:

    uri = resolve(entry_obj, 'media$group', 'media$content', 0, 'url')
    

    To simplify the calls a little, you might beef up the helper function to take a single string for the keys and split on spaces; that way you don’t have to type so many quotes, and we can also add a default value argument:

    def resolve(root, keys, default=None):
        for key in keys.split():
            try:
                root = root[key]
            except (TypeError, KeyError):
                try:
                    root = root[int(key)]
                except (IndexError, ValueError, KeyError):
                    return default
    
    uri = resolve(entry_obj, 'media$group media$content 0 url', '')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hopefully this is simple: I have a relatively long list where each list item
This is a long shot, I know... Let's say I have a collection List<MyClass>
In my implementation I have this: /// <inheritdoc cref=IInterface{T} this[,]/> public T this[long row,
How change int value in block, I have this : __block long long size
I have seen this for a long time, and finally decided to put the
If I have this entity: @Entity class Pet { @Id long id; public enum
I have this case <WrapPanel> <CheckBox>Really long name</CheckBox> <CheckBox>Short</CheckBox> <CheckBox>Longer again</CheckBox> <CheckBox>Foo</CheckBox> <Slider MinWidth=200
In Android 3.0 (Honeycomb) we have this feature that when we long press any
I have searched for this for a long time now... I am writing an
I have found this code for reverse geocoding: var point = new GLatLng (lat[1],long[1]);

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.