7
$\begingroup$

For example, there's a list {0, ..., 0} of length n. Can I replace the first six elements with the elements of another list of length 6?

$\endgroup$
0

8 Answers 8

4
$\begingroup$

There are many ways to do this. One direct way is to use Part with ;;

listA = {1, 2, 3, 4, 5, 6, 7, 8, 9};
listB = {a, b, c, d, e, f};
If[Length[listA] >= Length[listB],
 listA[[1 ;; Length[listB]]] = listB
 ,
 Abort[]
 ]

And now listA is

Mathematica graphics

But there could be a more functional way to do this in one line, I am sure. Notice that a list is immutable in Mathematica. So the above will actually generate new listA and not modify the original listA under the cover.

version 12.3.1 on windows 10.

$\endgroup$
6
$\begingroup$
Clear["Global`*"]

This will replace the first six positions of the first list with the complete second list for any length of the second list.

Format[a[n_]] := Subscript[a, n]
Format[b[n_]] := Subscript[b, n]

listA = Array[a, 10];
listB = Array[b, 6];

listC = ReplacePart[Partition[listA, UpTo[6]], 1 -> listB] // 
  Flatten

enter image description here

$\endgroup$
4
$\begingroup$
list1 = ConstantArray[0, 10];
list2 = Array[x, 6];

Several alternative methods:

PadRight[list2, Length@list1]

SubsetMap[list2 &, list1, Range @ 6]

ReplacePart[list1, Thread[Range @ 6 -> list2]]

Normal @ SparseArray[Range[6] -> list2, {10}]

all give

{x[1], x[2], x[3], x[4], x[5], x[6], 0, 0, 0, 0} 
$\endgroup$
4
$\begingroup$
la = {1, 2, 3, 4, 5, 6, 7, 8, 9};

lb = {a, b, c, d, e, f};

Another way using Drop and Prepend:

Prepend[Splice@lb]@Drop[la, Length@lb]

(*{a, b, c, d, e, f, 7, 8, 9}*)
$\endgroup$
3
$\begingroup$
alist = Range[1, 10]
blist = CharacterRange["a", "f"]

{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

{"a", "b", "c", "d", "e",> "f"}

Without length validation:

blist~Join~Take[alist, {7, -1}]

Take[blist, 6]~Join~Drop[alist, 6]

Join[blist, Last@TakeDrop[alist, 6]]

{blist, alist[[7 ;; -1]]} // Flatten

alist /. {(alist[[#]] -> blist[[#]] &) /@ Range[6]} // Flatten

would all yield:

{"a", "b", "c", "d", "e", "f", 7, 8, 9, 10}

$\endgroup$
3
$\begingroup$

If your initial lists are

list1={0,0,0,0,0,0,0,0,0,0};
list2={1,2,3,4,5,6};

you can perform the operation you want by

list1[[1;;6]]=list2;
$\endgroup$
2
  • 1
    $\begingroup$ Duplicates answer by Nasser. $\endgroup$
    – bbgodfrey
    Commented Oct 4, 2021 at 21:58
  • 1
    $\begingroup$ While both answers use the same underlying method, I would disagree that this is a duplicate. I just answered assuming that the author of the question knows the length of both lists, an there would be no need to check for that, nor to specify the length of the second list with Length. Definitely Nasser's answer is more versatile, but it is possible that user174967, or any other user, benefits from having a more straightforward, one line answer in here. $\endgroup$
    – anonymous
    Commented Oct 4, 2021 at 22:09
3
$\begingroup$
la = {1, 2, 3, 4, 5, 6, 7, 8, 9};
lb = {a, b, c, d, e, f};

Using Insert and Splice (new in 12.1)

Insert[la[[6 ;;]], Splice @ lb, 1]

{a, b, c, d, e, f, 6, 7, 8, 9}

$\endgroup$
2
$\begingroup$
listA = {1, 2, 3, 4, 5, 6, 7, 8, 9}
listB = {a, b, c, d, e, f}

Join[listB, listA[[1 + Length@listB ;;]]]

(* {a, b, c, d, e, f, 7, 8, 9} *)

An esoteric method:

Flatten[Reverse /@ Flatten[{listA, listB}, {{2}}], {{2}}] // First

(* {a, b, c, d, e, f, 7, 8, 9} *)
$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.