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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:20:22+00:00 2026-06-11T23:20:22+00:00

I am trying to write a function like zip. I am not good at

  • 0

I am trying to write a function like zip. I am not good at explaining what I mean, so i will just show ‘code’ of what i’m trying to do.

a = [1,2,3,[4,5]]
b = a[:]
zip(a, b) == [(1,1), (2,2), (3,3), ([4,5],[4,5])]
myzip(a, b) == [(1,1), (2,2), (3,3), [(4,4), (5,5)]]

I am so stuck on this it’s not even funny. I am trying to write it in a simple functional way with recursive lambdas, to make my code prettier. I want myzip like this because i want to use its output with another function I wrote which maps a function to a tree

def tree_map(func, tree):
    return map(lambda x: func(x) if not isinstance(x, list) else tree_map(func, x), 
               tree)

I have been trying to do something similar to this with zip, but I can’t seem to wrap my head around it. Does anyone have any ideas on how i could write myzip?

Edit: Look at tree_map! isn’t that pretty! i think so at least, but my mother tongue is Scheme 😛
and also, I want myzip to go as deep as it needs to. basically, I want myzip to retain the structure of the trees i pass it. Also, myzip will only handle trees that are the same shape.

  • 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-11T23:20:24+00:00Added an answer on June 11, 2026 at 11:20 pm

    I think the following should work:

    import collections
    
    def myzip(*args):
        if all(isinstance(arg, collections.Iterable) for arg in args):
            return [myzip(*vals) for vals in zip(*args)]
        return args
    

    Result:

    >>> a = [1,2,3,[4,[5,6]]]
    >>> b = [1,2,3,[4,[5,6]]]
    >>> myzip(a, b)
    [(1, 1), (2, 2), (3, 3), [(4, 4), [(5, 5), (6, 6)]]]
    

    Note that I use collections.Iterable instead of list in the type checking so that the behavior is more like zip() with tuples and other iterables.

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

Sidebar

Related Questions

I am trying to write some code that will function like google calendars quick
I'm trying to write my own function like with no luck. ...is not a
So, I was trying to write a function like this: void append_to_stream(std::ostream &stream) {
I'm trying to write a calendar function like this function get_date($month, $year, $week, $day,
i am trying to write one function which return sql statement like function get_sql($name=0,$date_start=0,$date_end=0)
I'm trying to write a let function that allows me to do things like:
I am trying to write a function that will allow me to browse for
I'm trying to write a small Powershell function that will return some summary data
I'm trying to write a function that does something like this: >>> Give-me the
I'm trying to write a function that accepts a variable number of parameters like

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.