How to insert elements at different locations of a given list
Clash Royale CLAN TAG#URR8PPP
up vote
8
down vote
favorite
Say a list is given as
list = a, b, c, d, r, m, n;
Suppose I want to insert 2 and 3 at position 3 and 7, respectively.
Insert[list, 2, 3]
puts 2 at position 3, but Insert[list, 2, 3, 3, 7]
does nothing.
How is it possible?
list-manipulation
add a comment |Â
up vote
8
down vote
favorite
Say a list is given as
list = a, b, c, d, r, m, n;
Suppose I want to insert 2 and 3 at position 3 and 7, respectively.
Insert[list, 2, 3]
puts 2 at position 3, but Insert[list, 2, 3, 3, 7]
does nothing.
How is it possible?
list-manipulation
2
Dirty trick:Block[k = 0, Insert[a, b, c, d, r, m, n, Unevaluated[2, 3[[++k]]], 3, 7]]
â J. M. is somewhat okay.â¦
Sep 27 at 13:02
@J.M. Wow, that one is really dirty. I had to read it several times to understand it half way at least...
â Henrik Schumacher
Sep 27 at 14:48
@Henrik, I didn't want to post it as an answer lest it be thought of as an endorsement. ;D
â J. M. is somewhat okay.â¦
Sep 27 at 14:52
related: Looking for a way to insert multiple elements into multiple positions simultaneously in a list
â user1066
Sep 28 at 0:07
add a comment |Â
up vote
8
down vote
favorite
up vote
8
down vote
favorite
Say a list is given as
list = a, b, c, d, r, m, n;
Suppose I want to insert 2 and 3 at position 3 and 7, respectively.
Insert[list, 2, 3]
puts 2 at position 3, but Insert[list, 2, 3, 3, 7]
does nothing.
How is it possible?
list-manipulation
Say a list is given as
list = a, b, c, d, r, m, n;
Suppose I want to insert 2 and 3 at position 3 and 7, respectively.
Insert[list, 2, 3]
puts 2 at position 3, but Insert[list, 2, 3, 3, 7]
does nothing.
How is it possible?
list-manipulation
list-manipulation
edited Sep 27 at 14:13
ÃÂûÃÂþñýôÃÂÿàÃÂõóó
2,339725
2,339725
asked Sep 27 at 12:52
Soumyajit Roy
1558
1558
2
Dirty trick:Block[k = 0, Insert[a, b, c, d, r, m, n, Unevaluated[2, 3[[++k]]], 3, 7]]
â J. M. is somewhat okay.â¦
Sep 27 at 13:02
@J.M. Wow, that one is really dirty. I had to read it several times to understand it half way at least...
â Henrik Schumacher
Sep 27 at 14:48
@Henrik, I didn't want to post it as an answer lest it be thought of as an endorsement. ;D
â J. M. is somewhat okay.â¦
Sep 27 at 14:52
related: Looking for a way to insert multiple elements into multiple positions simultaneously in a list
â user1066
Sep 28 at 0:07
add a comment |Â
2
Dirty trick:Block[k = 0, Insert[a, b, c, d, r, m, n, Unevaluated[2, 3[[++k]]], 3, 7]]
â J. M. is somewhat okay.â¦
Sep 27 at 13:02
@J.M. Wow, that one is really dirty. I had to read it several times to understand it half way at least...
â Henrik Schumacher
Sep 27 at 14:48
@Henrik, I didn't want to post it as an answer lest it be thought of as an endorsement. ;D
â J. M. is somewhat okay.â¦
Sep 27 at 14:52
related: Looking for a way to insert multiple elements into multiple positions simultaneously in a list
â user1066
Sep 28 at 0:07
2
2
Dirty trick:
Block[k = 0, Insert[a, b, c, d, r, m, n, Unevaluated[2, 3[[++k]]], 3, 7]]
â J. M. is somewhat okay.â¦
Sep 27 at 13:02
Dirty trick:
Block[k = 0, Insert[a, b, c, d, r, m, n, Unevaluated[2, 3[[++k]]], 3, 7]]
â J. M. is somewhat okay.â¦
Sep 27 at 13:02
@J.M. Wow, that one is really dirty. I had to read it several times to understand it half way at least...
â Henrik Schumacher
Sep 27 at 14:48
@J.M. Wow, that one is really dirty. I had to read it several times to understand it half way at least...
â Henrik Schumacher
Sep 27 at 14:48
@Henrik, I didn't want to post it as an answer lest it be thought of as an endorsement. ;D
â J. M. is somewhat okay.â¦
Sep 27 at 14:52
@Henrik, I didn't want to post it as an answer lest it be thought of as an endorsement. ;D
â J. M. is somewhat okay.â¦
Sep 27 at 14:52
related: Looking for a way to insert multiple elements into multiple positions simultaneously in a list
â user1066
Sep 28 at 0:07
related: Looking for a way to insert multiple elements into multiple positions simultaneously in a list
â user1066
Sep 28 at 0:07
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
7
down vote
How about this
myInsert[list_, valuePosList_] := Fold[Insert[#, Sequence @@ #2] &,
list,
SortBy[valuePosList, -Last[#] &]
]
myInsert[list, 2, 3, 3, 7]
a, b, 2, c, d, r, m, 3, n
add a comment |Â
up vote
5
down vote
Look what I found after spelunking:
GroupTheory`PermutationGroups`Private`FoldInsert[
a, b, c, d, r, m, n,
2, 3, 3, 7
]
a, b, 2, c, d, r, 3, m, n
Well, it is not entirely correct for it does not revert the order of insertion... =/
Wow! $phantom$
â J. M. is somewhat okay.â¦
Sep 27 at 14:53
1
I wonder if this package has similar functions.
â ÃÂûÃÂþñýôÃÂÿàÃÂõóó
Sep 27 at 15:16
@ÃÂûÃÂþñýôÃÂÿÃÂÃÂõóó That package appears to be very new. Good to know! (... although I won't find it applicable to my own work...)
â Henrik Schumacher
Sep 27 at 15:26
@HenrikSchumacher Yes, it is accompanied with a book published just about two months ago.
â ÃÂûÃÂþñýôÃÂÿàÃÂõóó
Sep 27 at 16:20
add a comment |Â
up vote
2
down vote
insertList[list_, valuePosList_] := ReplacePart[
list,
Apply[
Rule[#2, Sequence[#1, list[[#2]]]] &,
valuePosList
, 1
]
]
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
7
down vote
How about this
myInsert[list_, valuePosList_] := Fold[Insert[#, Sequence @@ #2] &,
list,
SortBy[valuePosList, -Last[#] &]
]
myInsert[list, 2, 3, 3, 7]
a, b, 2, c, d, r, m, 3, n
add a comment |Â
up vote
7
down vote
How about this
myInsert[list_, valuePosList_] := Fold[Insert[#, Sequence @@ #2] &,
list,
SortBy[valuePosList, -Last[#] &]
]
myInsert[list, 2, 3, 3, 7]
a, b, 2, c, d, r, m, 3, n
add a comment |Â
up vote
7
down vote
up vote
7
down vote
How about this
myInsert[list_, valuePosList_] := Fold[Insert[#, Sequence @@ #2] &,
list,
SortBy[valuePosList, -Last[#] &]
]
myInsert[list, 2, 3, 3, 7]
a, b, 2, c, d, r, m, 3, n
How about this
myInsert[list_, valuePosList_] := Fold[Insert[#, Sequence @@ #2] &,
list,
SortBy[valuePosList, -Last[#] &]
]
myInsert[list, 2, 3, 3, 7]
a, b, 2, c, d, r, m, 3, n
edited Sep 27 at 14:02
answered Sep 27 at 13:50
ÃÂûÃÂþñýôÃÂÿàÃÂõóó
2,339725
2,339725
add a comment |Â
add a comment |Â
up vote
5
down vote
Look what I found after spelunking:
GroupTheory`PermutationGroups`Private`FoldInsert[
a, b, c, d, r, m, n,
2, 3, 3, 7
]
a, b, 2, c, d, r, 3, m, n
Well, it is not entirely correct for it does not revert the order of insertion... =/
Wow! $phantom$
â J. M. is somewhat okay.â¦
Sep 27 at 14:53
1
I wonder if this package has similar functions.
â ÃÂûÃÂþñýôÃÂÿàÃÂõóó
Sep 27 at 15:16
@ÃÂûÃÂþñýôÃÂÿÃÂÃÂõóó That package appears to be very new. Good to know! (... although I won't find it applicable to my own work...)
â Henrik Schumacher
Sep 27 at 15:26
@HenrikSchumacher Yes, it is accompanied with a book published just about two months ago.
â ÃÂûÃÂþñýôÃÂÿàÃÂõóó
Sep 27 at 16:20
add a comment |Â
up vote
5
down vote
Look what I found after spelunking:
GroupTheory`PermutationGroups`Private`FoldInsert[
a, b, c, d, r, m, n,
2, 3, 3, 7
]
a, b, 2, c, d, r, 3, m, n
Well, it is not entirely correct for it does not revert the order of insertion... =/
Wow! $phantom$
â J. M. is somewhat okay.â¦
Sep 27 at 14:53
1
I wonder if this package has similar functions.
â ÃÂûÃÂþñýôÃÂÿàÃÂõóó
Sep 27 at 15:16
@ÃÂûÃÂþñýôÃÂÿÃÂÃÂõóó That package appears to be very new. Good to know! (... although I won't find it applicable to my own work...)
â Henrik Schumacher
Sep 27 at 15:26
@HenrikSchumacher Yes, it is accompanied with a book published just about two months ago.
â ÃÂûÃÂþñýôÃÂÿàÃÂõóó
Sep 27 at 16:20
add a comment |Â
up vote
5
down vote
up vote
5
down vote
Look what I found after spelunking:
GroupTheory`PermutationGroups`Private`FoldInsert[
a, b, c, d, r, m, n,
2, 3, 3, 7
]
a, b, 2, c, d, r, 3, m, n
Well, it is not entirely correct for it does not revert the order of insertion... =/
Look what I found after spelunking:
GroupTheory`PermutationGroups`Private`FoldInsert[
a, b, c, d, r, m, n,
2, 3, 3, 7
]
a, b, 2, c, d, r, 3, m, n
Well, it is not entirely correct for it does not revert the order of insertion... =/
answered Sep 27 at 14:50
Henrik Schumacher
40.9k258123
40.9k258123
Wow! $phantom$
â J. M. is somewhat okay.â¦
Sep 27 at 14:53
1
I wonder if this package has similar functions.
â ÃÂûÃÂþñýôÃÂÿàÃÂõóó
Sep 27 at 15:16
@ÃÂûÃÂþñýôÃÂÿÃÂÃÂõóó That package appears to be very new. Good to know! (... although I won't find it applicable to my own work...)
â Henrik Schumacher
Sep 27 at 15:26
@HenrikSchumacher Yes, it is accompanied with a book published just about two months ago.
â ÃÂûÃÂþñýôÃÂÿàÃÂõóó
Sep 27 at 16:20
add a comment |Â
Wow! $phantom$
â J. M. is somewhat okay.â¦
Sep 27 at 14:53
1
I wonder if this package has similar functions.
â ÃÂûÃÂþñýôÃÂÿàÃÂõóó
Sep 27 at 15:16
@ÃÂûÃÂþñýôÃÂÿÃÂÃÂõóó That package appears to be very new. Good to know! (... although I won't find it applicable to my own work...)
â Henrik Schumacher
Sep 27 at 15:26
@HenrikSchumacher Yes, it is accompanied with a book published just about two months ago.
â ÃÂûÃÂþñýôÃÂÿàÃÂõóó
Sep 27 at 16:20
Wow! $phantom$
â J. M. is somewhat okay.â¦
Sep 27 at 14:53
Wow! $phantom$
â J. M. is somewhat okay.â¦
Sep 27 at 14:53
1
1
I wonder if this package has similar functions.
â ÃÂûÃÂþñýôÃÂÿàÃÂõóó
Sep 27 at 15:16
I wonder if this package has similar functions.
â ÃÂûÃÂþñýôÃÂÿàÃÂõóó
Sep 27 at 15:16
@ÃÂûÃÂþñýôÃÂÿÃÂÃÂõóó That package appears to be very new. Good to know! (... although I won't find it applicable to my own work...)
â Henrik Schumacher
Sep 27 at 15:26
@ÃÂûÃÂþñýôÃÂÿÃÂÃÂõóó That package appears to be very new. Good to know! (... although I won't find it applicable to my own work...)
â Henrik Schumacher
Sep 27 at 15:26
@HenrikSchumacher Yes, it is accompanied with a book published just about two months ago.
â ÃÂûÃÂþñýôÃÂÿàÃÂõóó
Sep 27 at 16:20
@HenrikSchumacher Yes, it is accompanied with a book published just about two months ago.
â ÃÂûÃÂþñýôÃÂÿàÃÂõóó
Sep 27 at 16:20
add a comment |Â
up vote
2
down vote
insertList[list_, valuePosList_] := ReplacePart[
list,
Apply[
Rule[#2, Sequence[#1, list[[#2]]]] &,
valuePosList
, 1
]
]
add a comment |Â
up vote
2
down vote
insertList[list_, valuePosList_] := ReplacePart[
list,
Apply[
Rule[#2, Sequence[#1, list[[#2]]]] &,
valuePosList
, 1
]
]
add a comment |Â
up vote
2
down vote
up vote
2
down vote
insertList[list_, valuePosList_] := ReplacePart[
list,
Apply[
Rule[#2, Sequence[#1, list[[#2]]]] &,
valuePosList
, 1
]
]
insertList[list_, valuePosList_] := ReplacePart[
list,
Apply[
Rule[#2, Sequence[#1, list[[#2]]]] &,
valuePosList
, 1
]
]
answered Sep 27 at 15:55
rhermans
21.7k439104
21.7k439104
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f182704%2fhow-to-insert-elements-at-different-locations-of-a-given-list%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
2
Dirty trick:
Block[k = 0, Insert[a, b, c, d, r, m, n, Unevaluated[2, 3[[++k]]], 3, 7]]
â J. M. is somewhat okay.â¦
Sep 27 at 13:02
@J.M. Wow, that one is really dirty. I had to read it several times to understand it half way at least...
â Henrik Schumacher
Sep 27 at 14:48
@Henrik, I didn't want to post it as an answer lest it be thought of as an endorsement. ;D
â J. M. is somewhat okay.â¦
Sep 27 at 14:52
related: Looking for a way to insert multiple elements into multiple positions simultaneously in a list
â user1066
Sep 28 at 0:07