cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar
derchieh
Level II

Insert Into()

Hi,

 

I have a number of lists, of varying length, and I'd like to combine all the items in the lists into one list. 

For example: 

 

list1 = {"a", "b", "c"};
list2 = {"d", "e"};
list3 = {"f", "g", "h", "i"};
.... the number of lists ranges from 7 to 10.

 

Want to create

 

list = {"a", "b", "c", "d", "e"};

 

I have been creating the final list

 

Insert Into(list1, list2);
Insert Into(list1, list3);
.... and so on. 
(I don't mind the list items are added to list1)

 

Is there a more efficient way of doing this? Insert Into() doesn't seem to take more than 1 add-on parameter. 

Thank you.

1 REPLY 1
Craige_Hales
Super User

Re: Insert Into()

That is the best you can do if you are happy to update list1.

If you want a new list, leaving the old lists unchanged, the best choice between these

        lnew = l1 || l2 || l3;
// vs
        lnew = insert( l1, l2 ); lnew = insert( lnew, l3 );

depends on the length of the lists, though || eventually pulls ahead in the graph below.

 

All three are within a factor of 2 of the same speed. I like insertInto() because it avoids copying list1. I like the || concatenation operator because it is clear what it is doing.

 

InsertInto wins if you don't mind that it does what its name says.InsertInto wins if you don't mind that it does what its name says.

 

Craige