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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:26:13+00:00 2026-05-22T02:26:13+00:00

I have this multi-dimensional dict: a = {‘a’ : ‘b’, ‘c’ : {‘d’ :

  • 0

I have this multi-dimensional dict:

a = {'a' : 'b', 'c' : {'d' : 'e'}}

And written simple function to flatten that dict:

def __flatten(self, dictionary, level = []):
    tmp_dict = {}
    for key, val in dictionary.items():
        if type(val) == dict:
            tmp_dict.update(self.__flatten(val, level + [key]))
        else:
            tmp_dict['.'.join(level + [key])] = val
    return tmp_dict

After call this function with dict a i get in result:

{'a' : 'b', 'c.d' : 'e'}

Now, after making few instructions on this flattened dict i need to build new, multi-dimensional dict from that flattened. Example:

>> unflatten({'a' : 0, 'c.d' : 1))
{'a' : 0, 'c' : {'d' : 1}}

The only problem I have is that i do not have a function unflatten 🙂
Can anyone help with this? I have no idea how to do it.

EDIT:

Another example:

{'a' : 'b', 'c.d.e.f.g.h.i.j.k.l.m.n.o.p.r.s.t.u.w' : 'z'}

Should be after unflatten:

{'a': 'b', 'c': {'d': {'e': {'f': {'g': {'h': {'i': {'j': {'k': {'l': {'m': {'n': {'o': {'p': {'r': {'s': {'t': {'u': {'w': 'z'}}}}}}}}}}}}}}}}}}}

And another:

{'a' : 'b', 'c.d' : 'z', 'c.e' : 1}

To:

{'a' : 'b', 'c' : {'d' : 'z', 'e' : 1}}

This greatly increases the difficulty of the task, i know. Thats why i had problem with this and found no solution in hours..

  • 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-22T02:26:14+00:00Added an answer on May 22, 2026 at 2:26 am
    def unflatten(dictionary):
        resultDict = dict()
        for key, value in dictionary.items():
            parts = key.split(".")
            d = resultDict
            for part in parts[:-1]:
                if part not in d:
                    d[part] = dict()
                d = d[part]
            d[parts[-1]] = value
        return resultDict
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a multi-dimensional array right now that looks like this: function art_appreciation_feeds() {
I have a multi-dimensional array that looks like this: The base array is indexed
I have a multi-threaded Windows application that occasionally deadlocks. Inevitably this happens on a
I have a multi dimensional array, like this: array('name' => array('title'=>'Title','date'=>'Created')) I store it
Possible Duplicate: php multi-dimensional array remove duplicate I have an array like this: $a
So I have a multi-dimensional array looks like this. $config = array( First Name
I have a multi-dimensional array similar to the example below that I want to
I'm having a problem lately that's driving me crazy. I have a multi-dimensional array
I have a PHP function which populates a multi-dimensional array $client->getResponse() I want to
I have a multi-dimensional array that I am trying to feed into difflib.get_close_matches() .

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.