- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
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.)
result={"a"};
p={"b","c"};
result[N Items( result ) + 1] = p; // wordy, but intent is clear
show(result);
/*:
result = {"a", {"b", "c"}}; // good
p={"b","c"};
insertinto( result, p ) ; // looks right, but...
show(result);
/*:
result = {"a", "b", "c"}; // not what I want
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
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
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.