How to replace all occurrences of an element in a list?

Consider the following:

list={1,2,2,2,3};


I would like to replace all 2 with the string "Test". Replace[list,2->"Test"] does not do it.

Result must be: {1,"Test","Test","Test",3}.

-

You can use:

list /. {2 -> "Test"}

(* Out[]:= {1, "Test", "Test", "Test", 3} *)

-
Internally, a /. b becomes ReplaceAll[a,b] which by default, replaces all instances found. –  rcollyer Apr 19 '12 at 17:00
I prefere this one at the moment, as you can replace different figures at the same time, e.g. list/.{1->"A",2->"X"}. I wounder whether Replace is able of that too? –  John Apr 19 '12 at 17:01
@John It is: Replace[list, {1 -> "B", 2 -> "Test"}, {1}] –  Eli Lansey Apr 19 '12 at 17:02
@John note, that once a match is found, it stops looking, and refuses to apply a sequence of replacements, e.g. a /. {a -> b, b -> c} returns b. But, you can use ReplaceRepeated (//.), instead. a //. {a -> b, b -> c} then returns c. –  rcollyer Apr 19 '12 at 17:05

Replace by default works on the whole expression. You can add a level specification

list={1,2,2,2,3};
Replace[list,2->"Test", {1}]


Gives

{1, "Test", "Test", "Test", 3}

-
list = {1, 2, 2, 2, 3};
{"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.22942990064620972, "perplexity": 6109.771568401988}, "config": {"markdown_headings": false, "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-2014-15/segments/1397609530136.5/warc/CC-MAIN-20140416005210-00663-ip-10-147-4-33.ec2.internal.warc.gz"}