Message Boards Message Boards

0
|
4340 Views
|
3 Replies
|
0 Total Likes
View groups...
Share
Share this post:
GROUPS:

Stupid newbie question...

Posted 9 years ago

I believe i did my (re-)search, but found no solution to my problem. I have a list of things, say

list := {{1,2}, {1/2,1/3}, {3,4}}

now i want to replace a single element of my list with elements from a new list, say

replacement := {{4,5}, {6,7}, {1,2,3}}

the function ReplacePart looks sweet, but leaves on level of braces too much (the braces around {4,5}...{1,2,3}):

ReplacePart[list, 3-> replacement]
{{1, 2}, {1/2, 1/3}, {{4, 5}, {6, 7}, {1, 2, 3}}}

Flattening doesnt help - then I get individual elements inserted:

In[221]:= ReplacePart[list, 3 -> Flatten[replacement]]

Out[221]= {{1, 2}, {1/2, 1/3}, {4, 5, 6, 7, 1, 2, 3}}

I was looking for a function to 'unlist' what's around my replacement, but found nothing.

If there is someone around with a solution, you would make a poor newbie learn something for life...

Thank you in advance!

POSTED BY: Thomas Vogler
3 Replies

Is this what you want to do?

In[4]:= ReplacePart[list, 3 -> Sequence @@ replacement]

Out[4]= {{1, 2}, {1/2, 1/3}, {4, 5}, {6, 7}, {1, 2, 3}}
POSTED BY: Frank Kampas

Thank you David,

you made my day!

POSTED BY: Thomas Vogler

Not such a 'noob question'; I have to look up how to do this every other day!

You want the function Sequence; note that it takes a list of arguments, not a single list argument. The @@ below uses the elements of replacement as successive arguments for Sequence.

list = {{1, 2}, {1/2, 1/3}, {3, 4}};
replacement = {{4, 5}, {6, 7}, {1, 2, 3}};
ReplacePart[list, 3 -> Sequence @@ replacement]

Example result

POSTED BY: David Gathercole
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract