How to insert elements at different locations of a given list
Clash Royale CLAN TAG#URR8PPP
up vote
4
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
4
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.â¦
3 hours ago
@J.M. Wow, that one is really dirty. I had to read it several times to understand it half way at least...
â Henrik Schumacher
1 hour ago
@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.â¦
1 hour ago
add a comment |Â
up vote
4
down vote
favorite
up vote
4
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 1 hour ago
ÃÂûÃÂþñýôÃÂÿàÃÂõóó
2,204725
2,204725
asked 3 hours ago
Soumyajit Roy
1358
1358
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.â¦
3 hours ago
@J.M. Wow, that one is really dirty. I had to read it several times to understand it half way at least...
â Henrik Schumacher
1 hour ago
@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.â¦
1 hour ago
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.â¦
3 hours ago
@J.M. Wow, that one is really dirty. I had to read it several times to understand it half way at least...
â Henrik Schumacher
1 hour ago
@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.â¦
1 hour ago
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.â¦
3 hours ago
Dirty trick:
Block[k = 0, Insert[a, b, c, d, r, m, n, Unevaluated[2, 3[[++k]]], 3, 7]]
â J. M. is somewhat okay.â¦
3 hours ago
@J.M. Wow, that one is really dirty. I had to read it several times to understand it half way at least...
â Henrik Schumacher
1 hour ago
@J.M. Wow, that one is really dirty. I had to read it several times to understand it half way at least...
â Henrik Schumacher
1 hour ago
@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.â¦
1 hour ago
@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.â¦
1 hour ago
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
4
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
1
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... =/
Anyway, wow! (IOU one upvote.)
â J. M. is somewhat okay.â¦
1 hour ago
I wonder if this package has similar functions.
â ÃÂûÃÂþñýôÃÂÿàÃÂõóó
46 mins ago
@ÃÂûÃÂþñýôÃÂÿÃÂÃÂõóó That package appears to be very new. Good to know! (... although I won't find it applicable to my own work...)
â Henrik Schumacher
36 mins ago
add a comment |Â
up vote
0
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
4
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
4
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
4
down vote
up vote
4
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 2 hours ago
answered 2 hours ago
ÃÂûÃÂþñýôÃÂÿàÃÂõóó
2,204725
2,204725
add a comment |Â
add a comment |Â
up vote
1
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... =/
Anyway, wow! (IOU one upvote.)
â J. M. is somewhat okay.â¦
1 hour ago
I wonder if this package has similar functions.
â ÃÂûÃÂþñýôÃÂÿàÃÂõóó
46 mins ago
@ÃÂûÃÂþñýôÃÂÿÃÂÃÂõóó That package appears to be very new. Good to know! (... although I won't find it applicable to my own work...)
â Henrik Schumacher
36 mins ago
add a comment |Â
up vote
1
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... =/
Anyway, wow! (IOU one upvote.)
â J. M. is somewhat okay.â¦
1 hour ago
I wonder if this package has similar functions.
â ÃÂûÃÂþñýôÃÂÿàÃÂõóó
46 mins ago
@ÃÂûÃÂþñýôÃÂÿÃÂÃÂõóó That package appears to be very new. Good to know! (... although I won't find it applicable to my own work...)
â Henrik Schumacher
36 mins ago
add a comment |Â
up vote
1
down vote
up vote
1
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 1 hour ago
Henrik Schumacher
39.6k254118
39.6k254118
Anyway, wow! (IOU one upvote.)
â J. M. is somewhat okay.â¦
1 hour ago
I wonder if this package has similar functions.
â ÃÂûÃÂþñýôÃÂÿàÃÂõóó
46 mins ago
@ÃÂûÃÂþñýôÃÂÿÃÂÃÂõóó That package appears to be very new. Good to know! (... although I won't find it applicable to my own work...)
â Henrik Schumacher
36 mins ago
add a comment |Â
Anyway, wow! (IOU one upvote.)
â J. M. is somewhat okay.â¦
1 hour ago
I wonder if this package has similar functions.
â ÃÂûÃÂþñýôÃÂÿàÃÂõóó
46 mins ago
@ÃÂûÃÂþñýôÃÂÿÃÂÃÂõóó That package appears to be very new. Good to know! (... although I won't find it applicable to my own work...)
â Henrik Schumacher
36 mins ago
Anyway, wow! (IOU one upvote.)
â J. M. is somewhat okay.â¦
1 hour ago
Anyway, wow! (IOU one upvote.)
â J. M. is somewhat okay.â¦
1 hour ago
I wonder if this package has similar functions.
â ÃÂûÃÂþñýôÃÂÿàÃÂõóó
46 mins ago
I wonder if this package has similar functions.
â ÃÂûÃÂþñýôÃÂÿàÃÂõóó
46 mins ago
@ÃÂûÃÂþñýôÃÂÿÃÂÃÂõóó That package appears to be very new. Good to know! (... although I won't find it applicable to my own work...)
â Henrik Schumacher
36 mins ago
@ÃÂûÃÂþñýôÃÂÿÃÂÃÂõóó That package appears to be very new. Good to know! (... although I won't find it applicable to my own work...)
â Henrik Schumacher
36 mins ago
add a comment |Â
up vote
0
down vote
insertList[list_, valuePosList_] := ReplacePart[
list,
Apply[
Rule[#2, Sequence[#1, list[[#2]]]] &,
valuePosList
, 1
]
]
add a comment |Â
up vote
0
down vote
insertList[list_, valuePosList_] := ReplacePart[
list,
Apply[
Rule[#2, Sequence[#1, list[[#2]]]] &,
valuePosList
, 1
]
]
add a comment |Â
up vote
0
down vote
up vote
0
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 7 mins ago
rhermans
21.6k439104
21.6k439104
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.â¦
3 hours ago
@J.M. Wow, that one is really dirty. I had to read it several times to understand it half way at least...
â Henrik Schumacher
1 hour ago
@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.â¦
1 hour ago