cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar
Craige_Hales
Super User

How to insert a list into another list

In Iterate through all combinations of values from ARBITRARY number of lists I posted an answer that required inserting a list into another list of lists. The InsertInto function unpacks the inserted list into elements, which is not what I want. I used an assignment statement to the element one beyond the end of the list, which does insert the list as an element. Another choice is to wrap the list in another list before inserting it (so it unpacks the wrapped list, and inserts the original.)

 
Is there a better way to insert a list into a list? result and p are both lists.  
 
 result[N Items( result ) + 1] = p is a bit wordy but works and seems fairly clear that p is being stored just after the last element in the list. Good for extending, but can't insert anywhere else.
 

result={"a"};
p={"b","c"};
result[N Items( result ) + 1] = p; // wordy, but intent is clear
show(result);
/*:

result = {"a", {"b", "c"}}; // good

 

Insertinto( result, p ) won't work because it inserts p's elements, not p.
 
result={"a"};
p={"b","c"};
insertinto( result,  p ) ; // looks right, but...
show(result);
/*:
result = {"a", "b", "c"}; // not what I want
 
insertinto( result, evallist( { p } ) ) works, but seems less readable than the assignment. Can insert anywhere but makes an extra copy.
 
result={"a"};
p={"b","c"};
insertinto( result, evallist( { p } ) ) ; // makes an extra copy of the list and hard to read
show(result);
/*:
result = {"a", {"b", "c"}}; // good: what I want
 
 
edit: then I found Inserting List to List with JSL which is the same question. It looks like similar answers, leaning towards the evallist approach.
 
edit 18may2022: JMP Expr() weirdness, can anyone explain?  has a note about builtin function names, like list() or {} being different from unknown names.
Craige
2 ACCEPTED SOLUTIONS

Accepted Solutions

Re: How to insert a list into another list

FWIW I always do it the EvalList ( ) way in your 2nd example. I guess you could do it this way, and not evaluate until you want / need to... not sure if this is buying you much (?)

result = {"a"};
p = {"b", "c"};
Insert Into( result, {p} ); //don't evaluate p here... but of course p is non-static
show(result[2][1]);    //can still access elements of p if desired
Show( Eval List( result ) );  //or can eval to see the whole thing

brady_brady_0-1621344376429.png

 

 

View solution in original post

ih
Super User (Alumni) ih
Super User (Alumni)

Re: How to insert a list into another list

Not a direct solution but more as a work around I tend to use associative arrays, similar to what @pmroz suggested in your link.

View solution in original post

4 REPLIES 4

Re: How to insert a list into another list

FWIW I always do it the EvalList ( ) way in your 2nd example. I guess you could do it this way, and not evaluate until you want / need to... not sure if this is buying you much (?)

result = {"a"};
p = {"b", "c"};
Insert Into( result, {p} ); //don't evaluate p here... but of course p is non-static
show(result[2][1]);    //can still access elements of p if desired
Show( Eval List( result ) );  //or can eval to see the whole thing

brady_brady_0-1621344376429.png

 

 

Craige_Hales
Super User

Re: How to insert a list into another list

Yes. I've used the evallist() approach and always feel like it needs a comment to explain. In my original code (link at top) I keep changing p between insertions, so it can't just insert a reference to p.

Craige
ih
Super User (Alumni) ih
Super User (Alumni)

Re: How to insert a list into another list

Not a direct solution but more as a work around I tend to use associative arrays, similar to what @pmroz suggested in your link.

Craige_Hales
Super User

Re: How to insert a list into another list

I might use that next time. It looks simpler, even if it is using a more complicated (under the covers) data structure, and since I'm already generating an index with nitems(...)+1 ... yes, it might be the better choice.

Craige