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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T08:39:29+00:00 2026-06-02T08:39:29+00:00

I have a dictionary where entry values can reference another entry by key eventually

  • 0

I have a dictionary where entry values can reference another entry by key eventually ending with no entry for the current value or when “-” is encountered. The goal of this data structure is to find the parent for each entry and also transform “-” into None. For instance take:

d = {'1': '-', '0': '6', '3': '1', '2': '3', '4': '5', '6': '9'}
  • ‘1’ is a root that maps to ‘-‘ so it should result in None.
  • ‘0’ has a parent of ‘6’, which has a parent of ‘9’ so it should result in ‘9’.
  • ‘3’ has a parent of ‘1’, which maps to ‘-‘ so it should result in None.
  • ‘2’ has a parent of ‘3’,which has a parent of ‘1’ which maps to ‘-‘ so it should result in None.
  • ‘4’ should remain with parent of ‘5’
  • ‘6’ should remain with parent of ‘9’

My verbose solution is as follows:

d = {'1': '-', '0': '6', '3': '1', '2': '3', '4': '5', '6': '9'}
print(d)
for dis, rep in d.items():
    if rep == "-":
        d[dis] = None
        continue

    while rep in d:
        rep = d[rep]
        if rep == "-":
            d[dis] = None
            break
    else:
        d[dis] = rep
print(d)

The output is:

{'1': '-', '0': '6', '3': '1', '2': '3', '4': '5', '6': '9'}
{'1': None, '0': '9', '3': None, '2': None, '4': '5', '6': '9'}

The result is correct. The “1” element has no parent and the “2”/”3″ element point back to “1”. They should also have no parent.

Is there a terser pythonic way to accomplish this using Python 3+?

  • 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-02T08:39:30+00:00Added an answer on June 2, 2026 at 8:39 am

    To “walk” the dictionary, just do the lookups in a loop until there are no more:

    >>> def walk(d, val):
            while val in d:
                val = d[val]
            return None if val == '-' else val
    
    >>> d = {'1': '-', '0': '6', '3': '1', '2': '3', '4': '5', '6': '9'}
    >>> print {k: walk(d, k) for k in d}
    {'1': None, '0': '9', '3': None, '2': None, '4': '5', '6': '9'}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I cannot find a dictionary entry by key. I have an interface like the
Does VBA have dictionary structure? Like key<>value array?
I have a function that returns an entry on a dictionary, based on the
I have a Dictionary called AvailableBoxes. I want to lookup a string in this
I have this dictionary in my apps model file: TYPE_DICT = ( (1, Shopping
I have used Dictionary(TKey, TValue) for many purposes. But I haven't encountered any scenario
D is a dictionary whose entry values are of Type T What I'm attempting
I have a scenario where I have a propertybag of key/values that would look
I have a LINQ query which returns all the values inside a Dictionary, conditional
I have a dictionary that has a place name as a key and as

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.