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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:21:34+00:00 2026-05-24T19:21:34+00:00

I have a set of nested tuples: (‘id’, (‘name’, (‘name_float_fml’,)), (‘user’, (’email’,)), (‘user’, (‘last_login’,)))

  • 0

I have a set of nested tuples:

('id', ('name', ('name_float_fml',)), ('user', ('email',)), ('user', ('last_login',)))

I would like to combine lists with similar prefixes, resulting in:

('id', ('name', ('name_float_fml',)), ('user', ('email','last_login')))

Here is another example:

(('baz', ('bing', ('fizz', 'frozz', ('frazz', ('fry', 'bleep', 'blop'))))), ('baz', ('zap', ('zang',))), 'foo', 'bar')

would be merged to:

(('baz', (('bing', ('fizz', 'frozz', ('frazz', ('fry', 'bleep', 'blop')))), ('zap', ('zang')))), 'foo', 'bar')

These are intended to store paths from the root to the tree leaves:

  • ‘baz’ -> ‘bing’ -> ‘fizz’, aka. ('baz' ('bing' ('fizz,)))
  • ‘baz’ -> ‘zap’ -> ‘zang’, aka ('baz' ('zap', ('zang',)))
  • ‘baz’ -> ‘bing’ -> ‘frazz’ -> ‘blop’, aka ('baz', ('bing', ('frazz', ('blop,))))

I want to merge the elements where the leaves are reached by the same path. I hope this provides some amount of clarification.

I’ve written some code to do this, but it is ugly, verbose, and probably fragile. Is there some generic, concise, and/or efficient way of doing this? I imagine there may be some sort of itertools magic that I don’t know about which would provide some elegant solution.

Note: I’m using python 2.4

  • 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-24T19:21:34+00:00Added an answer on May 24, 2026 at 7:21 pm

    Here is a version that works for the examples you posted:

    a = ('id', ('name', ('name_float_fml',)), ('user', ('email',)), ('user', ('last_login',)))
    b = (('baz', ('bing', ('fizz', 'frozz',('frazz', ('fry', 'bleep', 'blop'))))), ('baz', ('zap', ('zang',))), 'foo', 'bar')
    
    def preserve_path(value):
        if len(value) == 2 and isinstance(value[1], (list, tuple)):
            return [value]
        else:
            return value
    
    def flatten_group(my_list):
        d = {}
        for item in my_list:
            # Only items with one string, followed by one tuple represent a path
            # segment. In all other situations, strings are leaves.
            if isinstance(item, (list, tuple)) and len(item) == 2:
                key, value = item
                if key in d:
                    d[key].extend(flatten_group(preserve_path(value)))
                else:
                    d[key] = preserve_path(list(flatten_group(value)))
            else:
                yield item
    
        for item in d.iteritems():
            yield item
    
    print list(flatten_group(a))
    # ['id', ('name', ['name_float_fml']), ('user', ['email', 'last_login'])]
    print list(flatten_group(b))
    # ['foo', 'bar', ('baz', [['bing', ('fizz', 'frozz', ('frazz', ('fry', 'bleep', 'blop')))], ('zap', ['zang'])])]
    

    Edit 3: Updated with the coauthored version that works for both examples, and incorporates your restriction that it only has to consider merging items that are tuples/lists and contain two items. This also prevents additional flattening of merged items.

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

Sidebar

Related Questions

I'd like to have the nested containers inherit that property, but when I set
In Rails 3 edge, I have set up two nested resources like this: config/routes.rb
I have a set of nested queries with express/mongoose, pretty much like so: app.get(...,
I have a set of nested ul's that look like this: <ul id=educationList> <li
I have a set of nested elements like such. <div id=master> <span id=num-1 class=num></span>
I have an set numeric ranges that I would like to optimize. Here's a
I have a set of nested unordered lists that represents page navigation. The list
I have a set of nested DIVs that slidetoggle using jQuery as the user
Is it possible to have nested set capabilities in this somewhat custom setup? Consider
I have your run of the mill nested set hierarchy type setup with the

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.