# How to replace every element in a list?

I have a list

s:={1, 2, 4, 7, 8}


and I wish to replace every element in the list with Range[Max[s]].

I know that I could replace every element individually using ReplaceAll by doing

Flatten[s /. {1 -> Range[Max[s]], 2 -> Range[Max[s]],
4 -> Range[Max[s]], 7 -> Range[Max[s]], 8 -> Range[Max[s]]}]


However, s is arbitrary, and I will be using this for lists much longer than s. I would like to be able to perform this with one command, rather than replacing each element individually.

I have tried

s /. s -> Range[Max[s]]


But this just returns the range once. I haven't been able to find anything about replacing every element of a list.

Is this acceptable?

s={1, 2, 4, 7, 8};
Flatten[Map[Range[Max[s]]&,s]]


which is the same as

Flatten[Range[Max[s]]&/@s]


and both produce

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


which matches what your example code produces.

Map or /@ will replace each item in the list with the result of a function applied to the item in the list. In your case the function doesn't depend on any individual item, but on the Max of the whole list. And the & turns Range[Max[s]] into a function. And the final enclosing Flatten gets rid of the nesting of the individual lists.

Does that help explain the thinking?

• Yes! Thank you! – user73377 Jun 17 '20 at 7:38

Using Replace(All) or Map is kinda slow for larger lists. Why not simply use:

ConstantArray[Range[Max[s]], Length[s]]


With ReplaceAll you need a pattern to restrict the replacement or it will match the entire list:

Flatten[s /. _Integer -> Range[Max@s]]

{1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, \
4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8}


## Performance

SHuisman's answer reminded me of performance. If that is important consider PadRight:

PadRight[Range[1], #*Length[s], Range@#] &[Max@s]


Range[1] is used instead of {} as the seed for PadRight because it creates a packed array.

Benchmark:

Needs["GeneralUtilities"]

fn1[s_List] := Flatten @ ConstantArray[Range[Max[s]], Length[s]]

fn2[s_List] := PadRight[Range[1], #*Length[s], Range@#] &[Max@s]

BenchmarkPlot[{fn1, fn2}, RandomInteger[9, #] &]
BenchmarkPlot[{fn1, fn2}, RandomInteger[#, 1*^5] &]


• PadRight is surprisingly fast! Wow! Flatten@, Join@@ or Catenate@ btw have quite different timings. but your PadRight stays faster… – SHuisman Jun 17 '20 at 12:33
Mod[Range[Length[s] #], #, 1] & @ Max[s]

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

First @ ArrayReshape[Range @ #, {1, Length[s] #}, "Periodic"] & @ Max[s]

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

(* Not a one-liner, but easy enough to do so *)
s = {1, 2, 4, 7, 8};

l = Length[s];

item = Range[Max[s]]

Replace[s, {_ -> item}, {1}]  (* use level spec *)

(* Flatten if desired *)

• l = Length[s]; does not appear to be used? Also consider Cases[s, _ -> item] as Cases defaults to a levelspec of {1}`. – Mr.Wizard Jun 20 '20 at 13:37
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.2827618718147278, "perplexity": 945.4509470338271}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2021-17/segments/1618038072082.26/warc/CC-MAIN-20210413031741-20210413061741-00580.warc.gz"}