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

  • Home
  • SEARCH
  • 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 6785605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:08:51+00:00 2026-05-26T17:08:51+00:00

How do I create a nested dictionary in python So, I want the data

  • 0

How do I create a nested dictionary in python
So, I want the data be in this form..

{Category_id: {Product_id:... productInstance},{prod_id_1: this instance}}

Basically if i do something like this
lets say I want to check whether the

product_id = 5 is in category 1.

so if I do

Dict[1].has_key(5)--> be true or false..

My bad code is

fin = readFile(db)
categoryDict = defaultdict(list)
itemDict ={}
for line in fin:
    itemInstance = setItemInstances(line)

    itemDict[itemInstance._product_id] = itemInstance
    categoryDict[itemInstance._category_id].append(itemDict)




EDIT: example
dict = {1: { p_id: p_instance_1,
           p_id_2: p_ins_2}
     2:{ p_in_this_cat_id: this isntance}}

THanks

  • 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-05-26T17:08:52+00:00Added an answer on May 26, 2026 at 5:08 pm

    I think this is closer to what you want:

    fin = readFile(db)
    categoryDict = defaultdict(dict)     # automatically create a subdict
    for line in fin:
        itemDict = {}                    # a new innermost dict for every item
        itemInstance = setItemInstances(line)
        itemDict[itemInstance._product_id] = itemInstance
        categoryDict[itemInstance._category_id] = itemDict
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a full inverted index in form of nested python dictionary. Its structure
I have a full inverted index in form of nested python dictionary. Its structure
I know you can create easily nested lists in python like this: [[1,2],[3,4]] But
I'm trying to read in data from and create a nested dictionary of dictionaries.
I need to create a multi-dimensional (nested) hashtable/dictionary so that I can use syntax
How would I create a nested list, I currently have this public function getNav($cat,$subcat){
I want to create a class with a nested enum. public class Foo {
I’m trying to create an instance of: java.awt.geom.Point2D.Double in ColdFusion. Point2D.Double is a nested
Is there any way to crate nested union types in F#? Something like this
I was wondering how we can create nested repeater control dynamically? I want to

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.