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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T01:28:56+00:00 2026-06-08T01:28:56+00:00

I have a large python dict created from json data and am creating a

  • 0

I have a large python dict created from json data and am creating a smaller dict from the large one. Some elements of the large dictionary have a key called ‘details’ and some elements don’t. What I want to do is check if the key exists in each entry in the large dictionary and if not, append the key ‘details’ with the value ‘No details available’ to the new dictionary. I am putting some sample code below just as a demonstration. The LargeDict is much larger with many keys in my code, but I’m keeping it simple for clarity.

LargeDict = {'results':
[{'name':'john','age':'23','datestart':'12/07/08','department':'Finance','details':'Good Employee'},
 {'name':'barry','age':'26','datestart':'25/08/10','department':'HR','details':'Also does payroll'},
 {'name':'sarah','age':'32','datestart':'13/05/05','department':'Sales','details':'Due for promotion'},
 {'name':'lisa','age':'21','datestart':'02/05/12','department':'Finance'}]}

This is how I am getting the data for the SmallDict:

SmallDict = {d['name']:{'department':d['department'],'details':d['details']} for d in LargeDict['results']}

I get a key error however when one of the large dict entries has no details. Am I right in saying I need to use the DefaultDict module or is there an easier way?

  • 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-08T01:28:58+00:00Added an answer on June 8, 2026 at 1:28 am

    Use the get(key, defaultVar) method to supply a default value when the 'details' key is missing:

    SmallDict = {d['name']:{'department':d['department'],'details':d.get('details','No details available')} for d in LargeDict['results']}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some large csv file, with about 200 header names (the first one
I have a Python datetime timestamp and a large dict (index) where keys are
I have some large data sets (numeric and textual) and as I'm studying and
I'm new to python, coming from matlab. I have a large sparse matrix saved
I am working on my first large python project. I have one function which
I have a relatively large dictionary in Python and would like to be able
I have a large Python dictionary of vectors (150k vectors, 10k dimensions each) of
I have some large json encoded files. The smallest is 300MB; the rest are
I have a large piece of Python 2 only code. It want to check
I have a large dataset that I am dealing with in Python. It is

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.