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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:32:26+00:00 2026-06-16T00:32:26+00:00

I have two lists lets say: def list1 = [a,b,c,d,e…] def list2 = [1,2,3,4,5…

  • 0

I have two lists lets say:

def list1 = [a,b,c,d,e...]
def list2 = [1,2,3,4,5... ]

I want them to mix in a certain pattern so that final list looks like:
[a,b,c,d,1,2,e,f,g,h,3,4,i,j,k,l,5,6...]

Basically after every n elements from list1, i get m elements from list2.

EDIT: If one list runs out of elements, the items in the remaining list should simply get added to the final list.

EDIT2: Both the lists can have objects as elements.

I want to find the most efficient way to solve this problem.

  • 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-16T00:32:27+00:00Added an answer on June 16, 2026 at 12:32 am

    Here’s one way of doing this in Groovy:

    So I have a method mix which takes a map with Integer keys (the number of elements required), and Lists as values:

    List mix( Map<Integer,List> amounts ) {
      amounts.collect { k, v ->
        v.collate( k )
      }.transpose().flatten()
    }
    

    Then, given:

    // The letters a to z
    def list1 = 'a'..'z'
    
    // The numbers 1 to 10
    def list2 = 1..10
    
    // Call, and ask for 4 of list1 followed by 2 of list2
    mix( [ 4:list1, 2:list2 ] )
    

    That returns:

    [ 'a', 'b', 'c', 'd', 1, 2,
      'e', 'f', 'g', 'h', 3, 4,
      'i', 'j', 'k', 'l', 5, 6,
      'm', 'n', 'o', 'p', 7, 8,
      'q', 'r', 's', 't', 9, 10 ]
    

    (formatted to look better here)

    As you can see, it runs out of numbers first, and when it does, the list ends. This is because transpose stops when one list runs out of elements.

    EDIT:

    Worked out another way with Iterators (so it’s lazy and won’t use up more memory than is otherwise required):

    class MixingIterator<T> implements Iterator<T> {
      private int idx = 0
      private List<Iterator> iter
      private List<Integer>  amts
      
      MixingIterator( List<List> lists, List<Integer> amounts ) {
        iter = lists*.iterator()
        int i = 0
        amts = amounts.collectMany { [ i++ ] * it }
        // OR FOR GROOVY 1.7.8
        // amts = amounts.collect { [ i++ ] * it }.flatten()
      }
      
      private void moveIdx() {
        idx = ++idx % amts.size()
      }
      
      @Override boolean hasNext() {
        iter*.hasNext().any()
      }
      
      @Override T next() {
        if( !hasNext() ) { throw new NoSuchElementException() }
        while( !iter[ amts[ idx ] ].hasNext() ) { moveIdx() }
        T ret = iter[ amts[ idx ] ].next()
        moveIdx()
        ret
      }
      
      @Override void remove() {
        throw new UnsupportedOperationException()
      }
    }
    

    You call it by:

    def list1 = 'a'..'z'
    def list2 = 1..10
    
    def ret = new MixingIterator( [ list1, list2 ], [ 4, 2 ] ).collect()
    // OR FOR GROOVY 1.7.8
    // def ret = new MixingIterator( [ list1, list2 ], [ 4, 2 ] ).collect { it }
    

    And ret will then equal:

    ['a', 'b', 'c', 'd', 1, 2,
     'e', 'f', 'g', 'h', 3, 4,
     'i', 'j', 'k', 'l', 5, 6,
     'm', 'n', 'o', 'p', 7, 8,
     'q', 'r', 's', 't', 9, 10,
     'u', 'v', 'w', 'x', 'y', 'z']
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say that I have two tables. The first is: table lists, with list_id
Lets say I have a function f[x_, y_] , and two lists l1 ,
Lets say that I have two tables. Table 1 is a list of vendors
I have two objects that are derived from same the base class. Lets say
Let's say that I have got two lists: temp<-c(con.sin.results,sin.results,exp.results) Temp<-c([,1:16],[,17:32],[,33:48],[,49:64]) Each of the variables
We have two lists, let's say students and their scores. I want to compare
In sql server, I have two tables (lets say Person and School). I want
Let's say I have two lists, l1 and l2. I want to perform l1
Lets say I have two Rails models, Users and Lists. Users have_many lists and
I have two lists: [a, b, c] [d, e, f] I want: [a, d,

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.