Products
  • Wolfram|One

    The definitive Wolfram Language and notebook experience

  • Mathematica

    The original technical computing environment

  • Wolfram Notebook Assistant + LLM Kit

    All-in-one AI assistance for your Wolfram experience

  • System Modeler
  • Wolfram Player
  • Finance Platform
  • Wolfram Engine
  • Enterprise Private Cloud
  • Application Server
  • Wolfram|Alpha Notebook Edition
  • Wolfram Cloud App
  • Wolfram Player App

More mobile apps

Core Technologies of Wolfram Products

  • Wolfram Language
  • Computable Data
  • Wolfram Notebooks
  • AI & Linguistic Understanding

Deployment Options

  • Wolfram Cloud
  • wolframscript
  • Wolfram Engine Community Edition
  • Wolfram LLM API
  • WSTPServer
  • Wolfram|Alpha APIs

From the Community

  • Function Repository
  • Community Paclet Repository
  • Example Repository
  • Neural Net Repository
  • Prompt Repository
  • Wolfram Demonstrations
  • Data Repository
  • Group & Organizational Licensing
  • All Products
Consulting & Solutions

We deliver solutions for the AI era—combining symbolic computation, data-driven insights and deep technical expertise

  • Data & Computational Intelligence
  • Model-Based Design
  • Algorithm Development
  • Wolfram|Alpha for Business
  • Blockchain Technology
  • Education Technology
  • Quantum Computation

WolframConsulting.com

Wolfram Solutions

  • Data Science
  • Artificial Intelligence
  • Biosciences
  • Healthcare Intelligence
  • Sustainable Energy
  • Control Systems
  • Enterprise Wolfram|Alpha
  • Blockchain Labs

More Wolfram Solutions

Wolfram Solutions For Education

  • Research Universities
  • Colleges & Teaching Universities
  • Junior & Community Colleges
  • High Schools
  • Educational Technology
  • Computer-Based Math

More Solutions for Education

  • Contact Us
Learning & Support

Get Started

  • Wolfram Language Introduction
  • Fast Intro for Programmers
  • Fast Intro for Math Students
  • Wolfram Language Documentation

More Learning

  • Highlighted Core Areas
  • Demonstrations
  • YouTube
  • Daily Study Groups
  • Wolfram Schools and Programs
  • Books

Grow Your Skills

  • Wolfram U

    Courses in computing, science, life and more

  • Community

    Learn, solve problems and share ideas.

  • Blog

    News, views and insights from Wolfram

  • Resources for

    Software Developers

Tech Support

  • Contact Us
  • Support FAQs
  • Support FAQs
  • Contact Us
Company
  • About Wolfram
  • Career Center
  • All Sites & Resources
  • Connect & Follow
  • Contact Us

Work with Us

  • Student Ambassador Initiative
  • Wolfram for Startups
  • Student Opportunities
  • Jobs Using Wolfram Language

Educational Programs for Adults

  • Summer School
  • Winter School

Educational Programs for Youth

  • Middle School Camp
  • High School Research Program
  • Computational Adventures

Read

  • Stephen Wolfram's Writings
  • Wolfram Blog
  • Wolfram Tech | Books
  • Wolfram Media
  • Complex Systems

Educational Resources

  • Wolfram MathWorld
  • Wolfram in STEM/STEAM
  • Wolfram Challenges
  • Wolfram Problem Generator

Wolfram Initiatives

  • Wolfram Science
  • Wolfram Foundation
  • History of Mathematics Project

Events

  • Stephen Wolfram Livestreams
  • Online & In-Person Events
  • Contact Us
  • Connect & Follow
Wolfram|Alpha
  • Your Account
  • User Portal
  • Wolfram Cloud
  • Products
    • Wolfram|One
    • Mathematica
    • Wolfram Notebook Assistant + LLM Kit
    • System Modeler
    • Wolfram Player
    • Finance Platform
    • Wolfram|Alpha Notebook Edition
    • Wolfram Engine
    • Enterprise Private Cloud
    • Application Server
    • Wolfram Cloud App
    • Wolfram Player App

    More mobile apps

    • Core Technologies
      • Wolfram Language
      • Computable Data
      • Wolfram Notebooks
      • AI & Linguistic Understanding
    • Deployment Options
      • Wolfram Cloud
      • wolframscript
      • Wolfram Engine Community Edition
      • Wolfram LLM API
      • WSTPServer
      • Wolfram|Alpha APIs
    • From the Community
      • Function Repository
      • Community Paclet Repository
      • Example Repository
      • Neural Net Repository
      • Prompt Repository
      • Wolfram Demonstrations
      • Data Repository
    • Group & Organizational Licensing
    • All Products
  • Consulting & Solutions

    We deliver solutions for the AI era—combining symbolic computation, data-driven insights and deep technical expertise

    WolframConsulting.com

    Wolfram Solutions

    • Data Science
    • Artificial Intelligence
    • Biosciences
    • Healthcare Intelligence
    • Sustainable Energy
    • Control Systems
    • Enterprise Wolfram|Alpha
    • Blockchain Labs

    More Wolfram Solutions

    Wolfram Solutions For Education

    • Research Universities
    • Colleges & Teaching Universities
    • Junior & Community Colleges
    • High Schools
    • Educational Technology
    • Computer-Based Math

    More Solutions for Education

    • Contact Us
  • Learning & Support

    Get Started

    • Wolfram Language Introduction
    • Fast Intro for Programmers
    • Fast Intro for Math Students
    • Wolfram Language Documentation

    Grow Your Skills

    • Wolfram U

      Courses in computing, science, life and more

    • Community

      Learn, solve problems and share ideas.

    • Blog

      News, views and insights from Wolfram

    • Resources for

      Software Developers
    • Tech Support
      • Contact Us
      • Support FAQs
    • More Learning
      • Highlighted Core Areas
      • Demonstrations
      • YouTube
      • Daily Study Groups
      • Wolfram Schools and Programs
      • Books
    • Support FAQs
    • Contact Us
  • Company
    • About Wolfram
    • Career Center
    • All Sites & Resources
    • Connect & Follow
    • Contact Us

    Work with Us

    • Student Ambassador Initiative
    • Wolfram for Startups
    • Student Opportunities
    • Jobs Using Wolfram Language

    Educational Programs for Adults

    • Summer School
    • Winter School

    Educational Programs for Youth

    • Middle School Camp
    • High School Research Program
    • Computational Adventures

    Read

    • Stephen Wolfram's Writings
    • Wolfram Blog
    • Wolfram Tech | Books
    • Wolfram Media
    • Complex Systems
    • Educational Resources
      • Wolfram MathWorld
      • Wolfram in STEM/STEAM
      • Wolfram Challenges
      • Wolfram Problem Generator
    • Wolfram Initiatives
      • Wolfram Science
      • Wolfram Foundation
      • History of Mathematics Project
    • Events
      • Stephen Wolfram Livestreams
      • Online & In-Person Events
    • Contact Us
    • Connect & Follow
  • Wolfram|Alpha
  • Wolfram Cloud
  • Your Account
  • User Portal
Wolfram Language & System Documentation Center
ReplacePart
  • See Also
    • Part
    • Set
    • Extract
    • ReplaceAt
    • SubsetMap
    • MapAt
    • FlattenAt
    • SparseArray
    • Insert
    • Delete
    • Nothing
    • Sequence
    • StringReplacePart
    • BitSet
    • NetReplacePart
  • Related Guides
    • Parts of Expressions
    • Parts of Matrices
    • Rearranging & Restructuring Lists
    • Handling Arrays of Data
    • Pattern Matching Functions
    • List Manipulation
    • Function Composition & Operator Forms
    • Structural Operations on Expressions
  • Tech Notes
    • Adding, Removing, and Modifying List Elements
    • Parts of Expressions
    • Manipulating Lists by Their Indices
    • See Also
      • Part
      • Set
      • Extract
      • ReplaceAt
      • SubsetMap
      • MapAt
      • FlattenAt
      • SparseArray
      • Insert
      • Delete
      • Nothing
      • Sequence
      • StringReplacePart
      • BitSet
      • NetReplacePart
    • Related Guides
      • Parts of Expressions
      • Parts of Matrices
      • Rearranging & Restructuring Lists
      • Handling Arrays of Data
      • Pattern Matching Functions
      • List Manipulation
      • Function Composition & Operator Forms
      • Structural Operations on Expressions
    • Tech Notes
      • Adding, Removing, and Modifying List Elements
      • Parts of Expressions
      • Manipulating Lists by Their Indices

ReplacePart[expr,inew]

yields an expression in which the i ^(th) part of expr is replaced by new.

ReplacePart[expr,{i1new1,i2new2,…}]

replaces parts at positions in by newn.

ReplacePart[expr,{i,j,…}new]

replaces the part at position {i,j,…}.

ReplacePart[expr,{{i1,j1,…}new1,…}]

replaces parts at positions {in,jn,…} by newn.

ReplacePart[expr,{{i1,j1,…},…}new]

replaces all parts at positions {in,jn,…} by new.

ReplacePart[inew]

represents an operator form of ReplacePart that can be applied to an expression.

Details and Options
Details and Options Details and Options
Examples  
Basic Examples  
Scope  
Generalizations & Extensions  
Options  
Heads  
Applications  
Properties & Relations  
Possible Issues  
See Also
Tech Notes
Related Guides
Related Links
History
Cite this Page
BUILT-IN SYMBOL
  • See Also
    • Part
    • Set
    • Extract
    • ReplaceAt
    • SubsetMap
    • MapAt
    • FlattenAt
    • SparseArray
    • Insert
    • Delete
    • Nothing
    • Sequence
    • StringReplacePart
    • BitSet
    • NetReplacePart
  • Related Guides
    • Parts of Expressions
    • Parts of Matrices
    • Rearranging & Restructuring Lists
    • Handling Arrays of Data
    • Pattern Matching Functions
    • List Manipulation
    • Function Composition & Operator Forms
    • Structural Operations on Expressions
  • Tech Notes
    • Adding, Removing, and Modifying List Elements
    • Parts of Expressions
    • Manipulating Lists by Their Indices
    • See Also
      • Part
      • Set
      • Extract
      • ReplaceAt
      • SubsetMap
      • MapAt
      • FlattenAt
      • SparseArray
      • Insert
      • Delete
      • Nothing
      • Sequence
      • StringReplacePart
      • BitSet
      • NetReplacePart
    • Related Guides
      • Parts of Expressions
      • Parts of Matrices
      • Rearranging & Restructuring Lists
      • Handling Arrays of Data
      • Pattern Matching Functions
      • List Manipulation
      • Function Composition & Operator Forms
      • Structural Operations on Expressions
    • Tech Notes
      • Adding, Removing, and Modifying List Elements
      • Parts of Expressions
      • Manipulating Lists by Their Indices

ReplacePart

ReplacePart[expr,inew]

yields an expression in which the i ^(th) part of expr is replaced by new.

ReplacePart[expr,{i1new1,i2new2,…}]

replaces parts at positions in by newn.

ReplacePart[expr,{i,j,…}new]

replaces the part at position {i,j,…}.

ReplacePart[expr,{{i1,j1,…}new1,…}]

replaces parts at positions {in,jn,…} by newn.

ReplacePart[expr,{{i1,j1,…},…}new]

replaces all parts at positions {in,jn,…} by new.

ReplacePart[inew]

represents an operator form of ReplacePart that can be applied to an expression.

Details and Options

  • Explicit negative part numbers count from the end. »
  • Part position specifications can be patterns.
  • Each pattern is effectively tested against each list of part numbers for parts in expr.
  • Patterns p (such as x_) that are not explicitly lists are treated as {p}.
  • Patterns can include constructs such as __, representing position specifications of variable lengths.
  • ReplacePart[expr,i:>new] can be used to replace a part without first evaluating it. With a rule such as patt:>new, new is evaluated separately for each position that matches patt.
  • With the default option setting Heads->Automatic, a head is replaced only when the corresponding position specification is explicitly 0.
  • With Heads->True a head is replaced whenever the corresponding position specification matches 0.
  • Heads->False never replaces heads.
  • ReplacePart can be used on SparseArray objects.
  • ReplacePart[i->new][expr] is equivalent to ReplacePart[expr,i->new].

Examples

open all close all

Basic Examples  (6)

Replace part 3:

Replace parts 2 and 5:

Replace part {2,1} of an array:

Replace parts whose positions match a pattern:

Replace parts in any expression:

Use a ReplacePart operator:

Scope  (11)

Replace a part 3 from the end:

Replace several parts by the same expression:

Part specifications can be patterns:

Replace every part except those with indices 1, 3, or 5:

Replace every part whose index is even:

Replace all elements in the first sublist:

Replace the last element in each sublist:

Replace elements on the diagonal:

Part specification patterns can contain variables that are used in the replacements:

Patterns can represent part lists of variable length:

Replace a part in an association:

Replace all values corresponding to key "x":

Replace the head of an association:

Generalizations & Extensions  (4)

The right-hand side of the rule is evaluated separately for each replacement done:

ReplacePart works with SparseArray objects:

ReplacePart works on heads:

Replace all heads by List:

Options  (3)

Heads  (3)

Replace all ordinary parts, but not heads:

Also replace heads:

Never replace heads:

Applications  (8)

Border a matrix with x's:

Highlight two squares in an array:

Generate a difference pattern for two cellular automaton initial conditions differing by one bit:

Insert a black cell at a random position at each step:

Successively replace parts of a list:

Successively replace disks in a graphic by circles:

Successively replace entries in a 2D array:

Replace elements whose indices are not relatively prime:

Properties & Relations  (5)

ReplacePart uses rules in the order given:

ReplacePart takes lists of positions in the same form as generated by Position:

ReplacePart takes the same part rules as SparseArray:

ReplacePart rewrites subexpressions at a particular position:

Replace rewrites parts that match a pattern at a particular level:

ReplacePart replaces parts of expressions whose positions match a pattern:

ReplaceAll replaces parts of expressions that themselves match a pattern:

Possible Issues  (4)

ReplacePart only affects parts that are already present:

Particularly in an Orderless function, the order of parts may change when they are replaced:

Replacing an empty list of positions does not change the expression:

Position {} corresponds to the whole expression:

Replacing the head of an association loses keys:

Extract rules from an association:

See Also

Part  Set  Extract  ReplaceAt  SubsetMap  MapAt  FlattenAt  SparseArray  Insert  Delete  Nothing  Sequence  StringReplacePart  BitSet  NetReplacePart

Function Repository: ReplaceAt  ReplacePartWith

Tech Notes

    ▪
  • Adding, Removing, and Modifying List Elements
  • ▪
  • Parts of Expressions
  • ▪
  • Manipulating Lists by Their Indices

Related Guides

    ▪
  • Parts of Expressions
  • ▪
  • Parts of Matrices
  • ▪
  • Rearranging & Restructuring Lists
  • ▪
  • Handling Arrays of Data
  • ▪
  • Pattern Matching Functions
  • ▪
  • List Manipulation
  • ▪
  • Function Composition & Operator Forms
  • ▪
  • Structural Operations on Expressions

Related Links

  • An Elementary Introduction to the Wolfram Language : Parts of Lists
  • NKS|Online  (A New Kind of Science)

History

Introduced in 1991 (2.0) | Updated in 1996 (3.0) ▪ 2003 (5.0) ▪ 2007 (6.0) ▪ 2014 (10.0)

Wolfram Research (1991), ReplacePart, Wolfram Language function, https://reference.wolfram.com/language/ref/ReplacePart.html (updated 2014).

Text

Wolfram Research (1991), ReplacePart, Wolfram Language function, https://reference.wolfram.com/language/ref/ReplacePart.html (updated 2014).

CMS

Wolfram Language. 1991. "ReplacePart." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2014. https://reference.wolfram.com/language/ref/ReplacePart.html.

APA

Wolfram Language. (1991). ReplacePart. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/ReplacePart.html

BibTeX

@misc{reference.wolfram_2025_replacepart, author="Wolfram Research", title="{ReplacePart}", year="2014", howpublished="\url{https://reference.wolfram.com/language/ref/ReplacePart.html}", note=[Accessed: 01-December-2025]}

BibLaTeX

@online{reference.wolfram_2025_replacepart, organization={Wolfram Research}, title={ReplacePart}, year={2014}, url={https://reference.wolfram.com/language/ref/ReplacePart.html}, note=[Accessed: 01-December-2025]}

Top
Introduction for Programmers
Introductory Book
Wolfram Function Repository | Wolfram Data Repository | Wolfram Data Drop | Wolfram Language Products
Top
  • Products
  • Wolfram|One
  • Mathematica
  • Notebook Assistant + LLM Kit
  • System Modeler

  • Wolfram|Alpha Notebook Edition
  • Wolfram|Alpha Pro
  • Mobile Apps

  • Wolfram Player
  • Wolfram Engine

  • Volume & Site Licensing
  • Server Deployment Options
  • Consulting
  • Wolfram Consulting
  • Repositories
  • Data Repository
  • Function Repository
  • Community Paclet Repository
  • Neural Net Repository
  • Prompt Repository

  • Wolfram Language Example Repository
  • Notebook Archive
  • Wolfram GitHub
  • Learning
  • Wolfram U
  • Wolfram Language Documentation
  • Webinars & Training
  • Educational Programs

  • Wolfram Language Introduction
  • Fast Introduction for Programmers
  • Fast Introduction for Math Students
  • Books

  • Wolfram Community
  • Wolfram Blog
  • Public Resources
  • Wolfram|Alpha
  • Wolfram Problem Generator
  • Wolfram Challenges

  • Computer-Based Math
  • Computational Thinking
  • Computational Adventures

  • Demonstrations Project
  • Wolfram Data Drop
  • MathWorld
  • Wolfram Science
  • Wolfram Media Publishing
  • Customer Resources
  • Store
  • Product Downloads
  • User Portal
  • Your Account
  • Organization Access

  • Support FAQ
  • Contact Support
  • Company
  • About Wolfram
  • Careers
  • Contact
  • Events
Wolfram Community Wolfram Blog
Legal & Privacy Policy
WolframAlpha.com | WolframCloud.com
© 2025 Wolfram
© 2025 Wolfram | Legal & Privacy Policy |
English