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 9263897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T13:41:43+00:00 2026-06-18T13:41:43+00:00

I have a list of dictionaries that have variable depth and different keys. for

  • 0

I have a list of dictionaries that have variable depth and different keys.

for example:

a = [{attr1: 1,
     attr2: {secondary_attr1: 'sometext',
             secondary_attr2: 'sometext',
             complex_attr: {more_attr: 999}}},
     {attr1: 2}]

I am using a class to collect all the information I need from each element of the list, but of course when I try to get an item from a key that is not in that particular dictionary, I get KeyError.

Here is how I am creating the instances for each element of the list a:

InsertLine(i['attr1'],i['attr2']['secondary_attr1'])

I tried to define a simple function but it doesn’t work:

def handle_keyerror(try_key):
    try:
        try_key
        return try_key
    except KeyError: 
        return 'NULL' 

I hope I made the probem clear.

  • 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-18T13:41:44+00:00Added an answer on June 18, 2026 at 1:41 pm

    Your function doesn’t work because it will always return try_key. Try this.

    def handle_keyerror(try_key):
        try:
            your_dictionary[try_key]
        except KeyError: 
            return None
        return try_key
    

    Anyway it is a bad idea to check it like that.

    You should rather use dict.get(key, default=None). If in dict there is no key key then it will return default value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list of dictionaries that have the same keys within eg: [{k1:'foo',
I have a List of Dictionaries that have keys of type string and values
I have a list of dictionaries that I get back from a web service
I have a list of dictionaries that looks something like this: toCSV = [{'name':'bob','age':25,'weight':200},{'name':'jim','age':31,'weight':180}]
I have a list of dictionaries that looks like this: [{'id':1,'name':'Foo'},{'id':2,'name':'Bar'}] I'd like to
I have a list of python dictionaries that look like this: sandwiches = [
I have a list called 'players' that consists of dictionaries. It looks like this:
I have a list of dictionaries like in this example: listofdict = [{'name': 'Foo',
I have a list of dictionaries, with keys 'a', 'n', 'o', 'u'. Is there
I have a dictionary and a list (of dictionaries) variables that I parsed some

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.