lreplace(n) Tcl Built-In Commands lreplace(n)

```

```

# [NAME](#NAME)

lreplace - Replace elements in a list with new elements

# [SYNOPSIS](#SYNOPSIS)

**lreplace list first last** ?*element ...*?

```

```

# [DESCRIPTION](#DESCRIPTION)

**lreplace** returns a new list formed by replacing zero or more elements of *list* with the *element* arguments. *first* and *last* are index values specifying the first and last elements of the range to replace. The index values *first* and *last* are interpreted the same as index values for the command **string index**, supporting simple index arithmetic and indices relative to the end of the list. 0 refers to the first element of the list, and **end** refers to the last element of the list.

If either *first* or *last* is less than zero, it is considered to refer to before the first element of the list. This allows **lreplace** to prepend elements to *list*. If either *first* or *last* indicates a position greater than the index of the last element of the list, it is treated as if it is an index one greater than the last element. This allows **lreplace** to append elements to *list*.

If *last* is less than *first*, then any specified elements will be inserted into the list before the element specified by *first* with no elements being deleted.

The *element* arguments specify zero or more new elements to be added to the list in place of those that were deleted. Each *element* argument will become a separate element of the list. If no *element* arguments are specified, then the elements between *first* and *last* are simply deleted.

# [EXAMPLES](#EXAMPLES)

Replacing an element of a list with another:

```
% lreplace {a b c d e} 1 1 foo
a foo c d e
```

Replacing two elements of a list with three:

```
% lreplace {a b c d e} 1 2 three more elements
a three more elements d e
```

Deleting the last element from a list in a variable:

```
% set var {a b c d e}
a b c d e
% set var [lreplace $var end end]
a b c d
```

A procedure to delete a given element from a list:

```
proc lremove {listVariable value} {


    upvar 1 $listVariable var


    set idx [lsearch -exact $var $value]


    set var [lreplace $var $idx $idx]
}
```

Appending elements to the list; note that **end+2** will initially be treated as if it is **6** here, but both that and **12345** are greater than the index of the final item so they behave identically:

```
% set var {a b c d e}
a b c d e
% set var [lreplace $var 12345 end+2 f g h i]
a b c d e f g h i
```

# [SEE ALSO](#SEE_ALSO)

[list(n)](/man/os/tcl/list.n), [lappend(n)](/man/os/tcl/lappend.n), [lassign(n)](/man/os/tcl/lassign.n), [ledit(n)](/man/os/tcl/ledit.n), [lindex(n)](/man/os/tcl/lindex.n), [linsert(n)](/man/os/tcl/linsert.n), [llength(n)](/man/os/tcl/llength.n), [lmap(n)](/man/os/tcl/lmap.n), [lpop(n)](/man/os/tcl/lpop.n), [lrange(n)](/man/os/tcl/lrange.n), [lremove(n)](/man/os/tcl/lremove.n), [lrepeat(n)](/man/os/tcl/lrepeat.n), [lreverse(n)](/man/os/tcl/lreverse.n), [lsearch(n)](/man/os/tcl/lsearch.n), [lseq(n)](/man/os/tcl/lseq.n), [lset(n)](/man/os/tcl/lset.n), [lsort(n)](/man/os/tcl/lsort.n), [string(n)](/man/os/tcl/string.n)

# [KEYWORDS](#KEYWORDS)

element, list, replace

7.4 Tcl

---

Document: lreplace(n) from tcl 9.1.0-r2 (Chainguard OS)
Source: https://downloads.sourceforge.net/project/tcl/Tcl/9.1.0/tcl9.1.0-src.tar.gz
