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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:51:57+00:00 2026-06-09T21:51:57+00:00

Disclaimer: Hello all Python masters and fans. I would like to thank everyone for

  • 0

Disclaimer: Hello all Python masters and fans. I would like to thank everyone for their caring support and dear advises which helped me so much. I am a Python newbie who is trying to learn and advance whilst keeping in mind the importance of best practices. Here’s a question in which I am seeking a swift way to avoid type checking as if there’s one thing I learnt here, that it’s not a good thing to do and there must be another way of doing it.

I am constructing a data-object to represent a site map.
I want this in memory so I can quickly map URLs before querying the DB.

Each node must have 2 elements. A name (of website’s section) and an ID (its ID in the DB) (4 to 8 digits normally but here represented with one digit only).

If this node has children (on the page), it has Name, ID and another dictionary representing the children.

I have decided to use the following for performance, ease of iteration and memory reasons:
I tried in the past only lists [name, id, [name, id, ..]], dictionaries and I think this is a not-such a bad way.

sitemap = {'section_one': 0,
           'section_two': [1, {'c_sect_2_1': 10,
                         'c_sect_2_2': [11, {'c_sect_2_2_1': 110,
                                           'c_sect_2_2_2': 111,
                                           'c_sect_2_2_3': 112}],
                          'c_sect_2_3': 12,
                          'c_sect_2_4': 13}],
           'section_three': 2,
           'section_four': 3,
           'section_five': 4}

I opted for lists because I might need to modify them (hence no tuples)
I am using dictionaries (hashable) and I can easily check if they contain a section.

Using this data-set, with below function, I map a URL (e.g. /section_two/c_sect_2_2/c_sect_2_2_3) and see if it exists or not to fetch the data from the DB. My function for this:

def map_url(url): #url here is a list e.g. ['section_two', 'c_sect_2_2', 'c_sect_2_2_3']
    sm = sitemap
    for e in url:
        if e in sm:
            if isinstance(sm[e], int):
                return sm[e] #e = where it stops matching due to no children
            sm = sm[e][1] #if not INT it's a list. list[1] has another dict to go-through
    return False #the URL could not be matched-mapped

My questions are:

  1. Instead of checking if value of an item in the dictionary is an
    integer to see whether it has children or not, what should I do?
    What can I do?
  2. What might be an alternative to this whole thing? (the way data-structure is built and/or the iteration through it)

I need this way of url mapping as my website can have a lot of nested sections and I don’t want to query the DB multiple times just to see if it exists or not.

Finally, I thank you all for your precious times and advises.

  • 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-09T21:51:59+00:00Added an answer on June 9, 2026 at 9:51 pm

    Instead of checking if value of an item in the dictionary is an integer to see whether it has children or not, what should I do? What can I do?

    The problem seems to be that you use different representations for sections with children and for sections without children. A section without children should really just be a section with an empty list of children:

    sitemap = {'section_one': [0, {}],
               'section_two': [1, {'c_sect_2_1': [10, {}],
                                   'c_sect_2_2': [11, {'c_sect_2_2_1': [110, {}],
                                                       'c_sect_2_2_2': [111, {}],
                                                       'c_sect_2_2_3': [112, {}]}],
                                   'c_sect_2_3': [12, {}],
                                   'c_sect_2_4': [13, {}]}],
               'section_three': [2, {}],
               'section_four': [3, {}],
               'section_five': [4, {}]}
    

    Now your code should become a bit simpler.

    What might be an alternative to this whole thing? (the way data-structure is built and/or the iteration through it)

    You could transform the sitemap into a flat dictionary at the start of your program, so that it becomes something like

    flat_sitemap = { 
        'section_one': 0,
        'section_two': 1,
        'section_two/c_sect_2_1': 10,
           # ...
        'section_two/c_sect_2_2/c_sect_2_2_1': 110
           # ...
        }
    

    That way your queries would work in expected O(1) time at the cost of a higher space usage.

    As for processing the original structure in a different way, you could use recursion. I often find it easier to formulate an algorithm on a tree-like structure in a recursive way, but it depends a bit on your way of thinking. Here’s an example (I’m assuming the format of sitemap that is shown in my first sample):

    def map_url(url, sm=[None, sitemap]):
        if not url: return sm[0]
        if url[0] not in sm[1]: return False
        return map_url(url[1:], sm[1][url[0]])
    
    print map_url(['section_two', 'c_sect_2_2', 'c_sect_2_2_3']) # => 112
    print map_url(['section_two', 'c_sect_2_2'])                 # => 10
    print map_url(['section_two', 'notexisting'])                # => False
    print map_url([])                                            # => None
    

    As you can see, this makes the special case explicit where you pass an empty URL. You should definitely think about what should happen in that particular case.

    You can even leave the second line of the function away. In that case a KeyError would be thrown if an URL cannot be matched (which seems sensible as well).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Disclaimer: I'm fairly new to python! If I want all the lines of a
I would like a regular expression, of which I will use with the Python
Disclaimer : I am relatively unfamiliar with the flash build processes, so some/all of
Disclaimer: I've looked through all the questions I can find and none of them
Disclaimer: I am new to python and django but have Drupal programming experience. I'm
Disclaimer While this question looked like a potential duplicate , it was resolved by
Disclaimer: I'm a noob. I've installed Python 3.2 (r32:88452) and ActiveTcl 8.5.9.2 (build 294317)
Disclaimer: I just made by hello world with gmcs yesterday Problem I want to
DISCLAIMER: The following code is not something I would ever use in a real
(first of all, a disclaimer: I'm a JavaScript / MooTools newbie, so it's very

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.