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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:15:38+00:00 2026-06-14T07:15:38+00:00

EDIT: See below for a suggested answer and how it’s not quite right yet.

  • 0

EDIT: See below for a suggested answer and how it’s not quite right yet.

There are many similar questions to this one on Stack Overflow, but none exactly like it in Python. I’m a programming novice, so please go easy.

I have a tree of nested dictionaries, like this:

[{'word': 'The',
  'next': [{'word': 'End',
            'next': None},
           {'word': 'quick',
            'next': [{'word': 'brown',
                      'next': [{'word': 'fox',
                                'next': None}]}]},
           {'word': 'best',
            'next': [{'word': 'of',
                      'next': [{'word': 'times',
                                'next': None}]}]}]}] 

I want to flatten all paths from top to bottom and end up with this:

[[{'word': 'The'},
  {'word': 'End'}],

 [{'word': 'The'},
  {'word': 'quick'},
  {'word': 'brown'},
  {'word': 'fox'}],

 [{'word': 'The'},
  {'word': 'best'},
  {'word': 'of'},
  {'word': 'times'}]]

I made a lovely little recursive function that created the original structure in the first place, but I’m having a hard time unrecursivizing it. This is as far as I got:

def flatten_combinations(result_tree, current_combo = None, all_combos = None):
    if current_combo is None:
        current_combo = []
    if all_combos is None:
        all_combos = []
    if result_tree is None:
        all_combos.append(current_combo)
        return
    for word in result_tree:
        current_combo.append({'word': word['word']})
        flatten_combinations(word['next'], current_combo, all_combos)
    return current_combo

…which returns this:

[{'word': 'The'},
 {'word': 'End'},
 {'word': 'quick'},
 {'word': 'brown'},
 {'word': 'fox'},
 {'word': 'best'},
 {'word': 'of'},
 {'word': 'times'}]

…which is clearly somewhat close, but not quite right.

I know that function is probably horribly un-Pythonic, but I’m teaching myself programming, so I’m not even trying to take advantage of possibly-existent language features that would let me elide over thinking through this stuff from scratch (” he said, posting to a Q&A site in the hope its members would help him elide a bit of thought).

So: what am I doing wrong?

EDIT: Moshe below corrected a couple of problems:

def flatten_combinations(result_tree, current_combo = None, all_combos = None):
    if current_combo is None:
        current_combo = []
    if all_combos is None:
        all_combos = []
    if result_tree is None:
        all_combos.append(current_combo)
        return
    for word in result_tree:
        current_combo = current_combo[:]
        current_combo.append({'word': word['word']})
        flatten_combinations(word['next'], current_combo, all_combos)
    return all_combos 

This is closer yet, but not quite right:

[{'word': 'The'}, 
 {'word': 'End'}],

[{'word': 'The'},
 {'word': 'End'},
 {'word': 'quick'},
 {'word': 'brown'},
 {'word': 'fox'}],

[{'word': 'The'},
 {'word': 'End'},
 {'word': 'quick'},
 {'word': 'best'},
 {'word': 'of'},
 {'word': 'times'}]]
  • 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-14T07:15:39+00:00Added an answer on June 14, 2026 at 7:15 am

    There are two minor mistakes in that:

    1) You return current_combo instead of all_combos. That only gives you your last result

    2) On each iteration, you modify current_combo. Make a copy first!

    new_current_combo = current_combo[:]
    new_current_combo.append({'word': word['word']})
    flatten_combinations(word['next'], new_current_combo, all_combos)
    

    Full code:

    def flatten_combinations(result_tree, current_combo=None, all_combos=None):
        if current_combo is None:
            current_combo = []
        if all_combos is None:
            all_combos = []
        if result_tree is None:
            all_combos.append(current_combo)
            return
        for word in result_tree:
            new_current_combo = current_combo[:]
            new_current_combo.append({'word': word['word']})
            flatten_combinations(word['next'], new_current_combo, all_combos)
        return all_combos 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

EDIT: See my answer below--> I am wanting to have a view that when
EDIT: This problem has been solved. See below. Hey all. I'm building an iPhone
This question was initially misphrased, see the EDIT below. I'll leave it up for
[EDIT: Problem solved. Please see my answer below.] In my app I call the
((Answer selected - see Edit 5 below.)) I need to write a simple pink-noise
I have a Controller with two Edit methods (see below). When I submit the
In my application I have an edit profile page which you can see below;
PLEASE SEE THE EDIT BELOW, THE REPORT SEEMS TO USE CACHED DATA? I cant
EDIT: See this in action here: http://jsbin.com/emobi/5 -- and that's using mouseenter/mouseleave. I have
Intro: EDIT: See solution at the bottom of this question (c++) I have 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.