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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:11:30+00:00 2026-05-14T06:11:30+00:00

I have a nested list something like this: PLACES = ( (‘CA’, ‘Canada’, (

  • 0

I have a nested list something like this:

PLACES = (
    ('CA', 'Canada', (
        ('AB', 'Alberta'),
        ('BC', 'British Columbia' (
            ('van', 'Vancouver'),
        ),
        ...
    )),
    ('US', 'United States', (
        ('AL', 'Alabama'),
        ('AK', 'Alaska'),
        ...

I need to retrieve some data out of it. If depth is 0 I need to retrieve all the countries (and their codes), if depth == 1, I need to retrieve all the states/provinces, if depth == 2 I need to retrieve all the cities… and so forth. Is there some set library for doing stuff like this? Or can someone point me in the right direction? I started coding up a solution only to realize it wouldn’t work for levels deeper than 1 because you have to go in and out of each list…

Also notice that not all items have a 3rd part (i.e., we’re pretending Alberta doesn’t have any cities, so retrieving items at depth 2 would just return ('van','Vancouver') in this limited scenario).


I didn’t realize this before, but I also needed the parent value. So, I modified interjay’s solution:

def depth_gen(seq, depth, par=None):
    if depth==0:
        for x in seq:
            yield par, x[0], x[1] 
        return

    for x in seq:
        if len(x)==3:
            par = x[0]
            for y in depth_gen(x[2], depth-1, par):
                yield y

Which I’m using to generate some HTML:

<label for="id-pickup_address-province">Province</label>
<select id="id-pickup_address-province" rel="pickup_address-country" name="pickup_address-province">
    <option rel="CA" value="AB">Alberta</option>
    <option rel="CA" value="BC">British Columbia</option>
    <option rel="CA" value="MB">Manitoba</option>
    ...
    <option rel="US" value="WV">West Virginia</option>
    <option rel="US" value="WI">Wisconsin</option>
    <option rel="US" value="WY">Wyoming</option>
</select>
<label for="id-pickup_address-country">Country</label>
<select id="id-pickup_address-country" name="pickup_address-country">
    <option value="CA">Canada</option>
    <option value="US">United States</option>
</select>

And then I should be able to easily filter the list with jQuery based on which country is selected…

  • 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-14T06:11:31+00:00Added an answer on May 14, 2026 at 6:11 am

    Here is a solution which will work for any depth:

    def depthGenerator(seq, depth):
        if depth==0:
            for x in seq:
                yield x[:2] #strip subsequences
            return
    
        for x in seq:
            if len(x)==3:   #has subsequence?
                for y in depthGenerator(x[2], depth-1):
                    yield y
    

    Example:

    >>> list(depthGenerator(PLACES, 1))
    [('AB', 'Alberta'), ('BC', 'British Columbia'), ('AL', 'Alabama'), ('AK', 'Alaska')]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a nested list with subitem numbering, like this: 1. Item 1 1.1
In Python you can have multiple iterators in a list comprehension, like [(x,y) for
I have a nested data object for a set of items within categories. Each
I am working on a front-end web app where a nested unordered list would
I have a configurable report. For each field that can be included on the
I've got some financial data to store and manipulate. Let's say I have 2
Similar to my other question : I have a ListView bound to a Dictionary.
This is actually a problem that I've already solved, but I suspect that there
I'm stumbling over myself trying to get a seemingly simple thing accomplished. I have

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.