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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:23:32+00:00 2026-05-24T09:23:32+00:00

I have a 2-dimensional list of named tuples (let’s say that each tuple has

  • 0

I have a 2-dimensional list of named tuples (let’s say that each tuple has N values), and I want to unpack them into N different 2-dimensional lists where each unpacked 2-D list is composed entirely of a single attribute from the original list. For example if I have this 2-D list:

>>> combo = namedtuple('combo', 'i, f, s')
>>> combo_mat = [[combo(i + 3*j, float(i + 3*j), str(i + 3*j)) for i in range(3)]
                 for j in range(3)]
>>> combo_mat
[[combo(i=0, f=0.0, s='0'), combo(i=1, f=1.0, s='1'), combo(i=2, f=2.0, s='2')],
 [combo(i=3, f=3.0, s='3'), combo(i=4, f=4.0, s='4'), combo(i=5, f=5.0, s='5')],
 [combo(i=6, f=6.0, s='6'), combo(i=7, f=7.0, s='7'), combo(i=8, f=8.0, s='8')]]

I’d like the 3 results to be:

[[0, 1, 2],
 [3, 4, 5],
 [6, 7, 8]]

[[0.0, 1.0, 2.0],
 [3.0, 4.0, 5.0],
 [6.0, 7.0, 8.0]]

[['0', '1', '2'],
 ['3', '4', '5'],
 ['6', '7', '8']]

If I just had a 1-dimensional list of tuples I’d use zip(*mylist), like:

>>> zip(*[combo(i=0, f=0.0, s='0'), combo(i=1, f=1.0, s='1'), combo(i=2, f=2.0, s='2')])
[(0, 1, 2), (0.0, 1.0, 2.0), ('0', '1', '2')]

And I can extend this to my situation just by nesting:

>>> zip(*[zip(*combs) for combs in combo_mat])
[((0, 1, 2),
  (3, 4, 5),
  (6, 7, 8)),
 ((0.0, 1.0, 2.0),
  (3.0, 4.0, 5.0),
  (6.0, 7.0, 8.0)),
 (('0', '1', '2'),
  ('3', '4', '5'),
  ('6', '7', '8'))]

But this doesn’t give me the lists I wanted, and nested unpacking zip(*) functions isn’t that readable. Anyone have any ideas for a more pythonic solution? Bonus points if you can work the names of the tuples’ attributes in there somewhere in the end result.

Actually, now that I think of it, it would be ideal if I could have a dict that mapped the name of the tuple attribute to its respective matrix, like:

{'i': [[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]], 
 'f': [[0.0, 1.0, 2.0],
       [3.0, 4.0, 5.0],
       [6.0, 7.0, 8.0]]
 's': [['0', '1', '2'],
       ['3', '4', '5'],
       ['6', '7', '8']]}
  • 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-24T09:23:34+00:00Added an answer on May 24, 2026 at 9:23 am

    Functional programming to the rescue? It’s essentially a cleaner version of nesting the zips:

    def fmap_element(f, el):
        return f(el)
    
    def fmap_list(f, l):
        return [fmap_element(f, el) for el in l)]
    
    def fmap_lol(f, lol):
        return [fmap_list(f,l) for l in lol]
    
    def split_nt_lol(nt_lol):
        return dict((name, fmap_lol(lambda nt: getattr(nt, name), nt_lol)) 
                    for name in nt_lol[0][0]._fields)
    

    Usage:

    >>> split_nt_lol(combo_mat)
    {'i': [[0, 1, 2], [3, 4, 5], [6, 7, 8]], 
     's': [['0', '1', '2'], ['3', '4', '5'], ['6', '7', '8']], 
     'f': [[0.0, 1.0, 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]]}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have constructed two dictionary that contains histogram values of images. Each dictionary has
I have a space-separated list of coordinate tuples, with arbitrarily many tuples. Each tuple
Say I have a list: L = [2,5,1,8,3,7,9,4,6,10] or something similar And I want
I have a more dimensional php array that i want to pass to a
Let's say I have a list of an object like this: var users =
I have a multi-dimensional array, which basically consists of one sub-array for each year.
I have an 2 dimensional array. I write 3 values into the array during
I have a multi dimensional array. The only actual values (other than other arrays)
I have a div that contains a paragraph, a list and a right-floated div.
I have a tuple of tuples - for example: tupleOfTuples = ((1, 2), (3,

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.