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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:44:58+00:00 2026-05-27T09:44:58+00:00

After searching around for quite a while and trying to come up with a

  • 0

After searching around for quite a while and trying to come up with a solution on my own with list comprehensions, I feel like I’ve been completely stumped on how to solve the following problem:

Given a list:

crazy_list = [a, b]

Where:

a = [['*'], ['*', '34', '*', '*', '*', '*', '*', '*', '*'], ['*', '*', '*', '102', '*', '*', '*', '*', '*'], ['*', '*', '*', '*', '*', '170', '*', '*', '*'], ['*', '*', '*', '*', '*', '*', '*', '238', '*'], ['*']]
b = [['*'], ['*', '*', '*', '102', '*', '*', '*', '*', '*'], ['*', '*', '*', '*', '*', '170', '*', '*', '*'], ['*', '*', '*', '*', '*', '*', '*', '238', '*'], ['*', '34', '*', '*', '*', '*', '*', '*', '*'], ['*']]

How do I elegantly get a new list of lists such that:

answer = [['*'], ['*', '34', '*', '102', '*', '*', '*', '*', '*'], ['*', '*', '*', '102', '*', '170', '*', '*', '*'], ['*', '*', '*', '*', '*', '170', '*', '238', '*'], ['*', '34', '*', '*', '*', '*', '*', '238', '*'], ['*']]

and how would I generalize this kind of merging to do the same for:

[a, b, c, etc...]

(c and onwards would have the same number of elements and subelements as ‘a’ and ‘b’, but would probably have different unique values in different positions)

====================v The little that I’ve been able to do so far v=====================

I can come up with a list comprehension that does do what I want for individual lists with something like:

c = ['*', '34', '*', '*', '*', '*', '*', '*', '*']
d = ['*', '*', '*', '102', '*', '*', '*', '*', '*']

by doing:

[c[item_indx] if item != '*' else d[item_indx] for item_indx, item in enumerate(c)]

but even trying to generalize what I have so far is giving me one heck of a headache…
I’m probably approaching this problem incorrectly. Some help and/or thoughts on how to solve/better-approach this problem would be much appreciated. Also, I can’t just ditch the ‘*’s as they encode important timing information. Thanks again for your time!

  • 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-27T09:44:59+00:00Added an answer on May 27, 2026 at 9:44 am

    First, let’s figure out how to merge a sublist from a with a sublist from b:

    [x if x != '*' else y for (x, y) in zip(a, b)]
    

    We take pairs of items from the two lists, and for each pair, produce one or the other element, and make a list of the results.

    To generalize that to multiple lists: well, zip accepts *args, so there’s no problem there – we can make tuples of items from any number of lists. But then we need to take “the non-‘*’ element, if it exists, otherwise ‘*'” from that tuple.

    One way to do that is to make a set of the elements, remove '*', and use any element from the result (if non-empty), otherwise use '*'. We can get “any element” from a set with .pop(), which will raise a KeyError if the set is empty. So we make a wrapper function and then use it:

    def non_star_if_possible(items):
        try: return set(items).difference('*').pop()
        except KeyError: return '*'
    
    def merge(lists):
        return [non_star_if_possible(items) for items in zip(*lists)]
    

    Finally, we actually have a list of lists of lists, and we want to take lists from the lists of lists, element-wise. So we apply the same zip trick:

    def merge_all(data):
        return [merge(lists) for lists in zip(*data)]
    

    And if we prefer, we could fold those last two into a single nested list comprehension, but you might find it clearer this way. 🙂

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

Sidebar

Related Questions

I have been searching around for quite a while on ways to do this,
This seems like an elementary question, but after a lot of searching around I
I am trying to understand how classpath really works. After searching around the web
After significant bit of searching around, I am still feeling quite clueless as to
I hope this isn't a stupid question, but after searching around for quite a
I have been searching around and it looks like this question has been asked
I've been searching around trying to find an answer both here and google, although
After searching around a bit and consulting the Dinosaur Book, I've come to SO
I am really posting this out of desperation after searching around a lot for
Hey guys, I've been browsing around on SO for quite some time and can't

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.