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

The Archive Base Latest Questions

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

I have a list of data from which I am creating dict inside dict,

  • 0

I have a list of data from which I am creating dict inside dict, structure it is coming as expected but somewhere it is overwriting, I don’t know where

a=['t1_h1','t2_h2']
b=['h1_d1','h1_d2','h2_d3']
c=['d1_dom1','d2_dom2','d3_dom3']
d=['dom1_a','dom1_b','dom2_a','dom2_b','dom3_a','dom3_b']

I tried using this code

for item in a:
f[item.split('_')[0]]={}
for hypercube in b:
    if item.split('_')[1] in hypercube:#h1 in b

        f[item.split('_')[0]][item.split('_')[1]]={}

        for dimension in c:
            if hypercube.split('_')[1] in dimension:#d1 in c 
                f[item.split('_')[0]][item.split('_')[1]][hypercube.split('_')[1]]={}
                for domain in d:
                    if dimension.split('_')[1] in domain:#dom1 in d
                        if f[item.split('_')[0]][item.split('_')[1]][hypercube.split('_')[1]].has_key(dimension.split('_')[1]):
                            f[item.split('_')[0]][item.split('_')[1]][hypercube.split('_')[1]][dimension.split('_')[1]].append(domain.split('_')[1])
                        else:
                            f[item.split('_')[0]][item.split('_')[1]][hypercube.split('_')[1]][dimension.split('_')[1]]=[domain.split('_')[1]]

Actually I am trying to print in this format:

{'t1': {'h1': {'d1': {'dom1': ['a', 'b']}, 'd2': {'dom2': ['a', 'b']}}},
     't2': {'h2': {'d3': {'dom3': ['a', 'b']}}}}

But the output I am getting is:

{'t2': {'h2': {'d3': {'dom3': ['a','b']}}}, 't1': {'h1': {'d2': {'dom2': ['a','b']}}}}

but after changing last thing is in ‘t1’ ‘d1’ value is missing

  • 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-05T13:19:36+00:00Added an answer on June 5, 2026 at 1:19 pm

    You don’t want to nest your for loops, you want to nest the for searches as you process each list a, b, c and d, adding deeper and deeper levels to f. This gives your desired output:

    f = {}
    for item in a:
        ak1,ak2 = item.split('_')
        f[ak1] = {ak2:{}}
    for item in b:
        bk1,bk2 = item.split('_')
        next(f[akey][bk1] 
                for akey in f 
                    if bk1 in f[akey])[bk2] = {}
    for item in c:
        ck1,ck2 = item.split('_')
        next(f[akey][bkey][ck1] 
                for akey in f 
                    for bkey in f[akey] 
                        if ck1 in f[akey][bkey])[ck2] = []
    for item in d:
        dk1,dk2 = item.split('_')
        next(f[akey][bkey][ckey][dk1] 
                for akey in f 
                    for bkey in f[akey]
                        for ckey in f[akey][bkey]
                            if dk1 in f[akey][bkey][ckey]).append(dk2)
    
    import pprint
    pprint.pprint(f)
    

    prints:

    {'t1': {'h1': {'d1': {'dom1': ['a', 'b']}, 'd2': {'dom2': ['a', 'b']}}},
     't2': {'h2': {'d3': {'dom3': ['a', 'b']}}}}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have already created a webpart to show the data from list, but I
I have a custom list view that is getting data from server and changing
I have a test list that I am trying to capture data from using
I have looped data from mysql, and it's a pretty long list. What's the
I have some C structures related to a 'list' data structure. They look like
I have contact list which contains (checkbox,firstname,lastname,phone). I am creating this list using html
I am creating a module to graphically visualize workflows using raphael,which take data from
I have a class A which is responsible for fetching data from web services
I am creating an asp.net page which loads data from a (SQL) visual studio
I have a list of data that is a schedule. Each item has a

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.