ZetCode

Tcl linsert Command

last modified April 3, 2025

The Tcl linsert command inserts elements into a list at a specified index position. It returns a new list with the elements inserted, leaving the original list unchanged.

Basic Definition

The linsert command creates a new list by inserting one or more elements into an existing list at a given position. The original list remains unmodified.

Syntax: linsert list index element ?element ...?. The command takes a list, an index position, and one or more elements to insert.

Inserting a Single Element

This example demonstrates inserting a single element into a list at position 1.

basic_linsert.tcl
set colors {red green blue}
set new_colors [linsert $colors 1 yellow]
puts $new_colors

This creates a new list with "yellow" inserted between "red" and "green". The original colors list remains unchanged. The output is "red yellow green blue".

Inserting at the Beginning

To insert elements at the start of a list, use index 0.

linsert_beginning.tcl
set numbers {2 3 4}
set new_numbers [linsert $numbers 0 1]
puts $new_numbers

This inserts the number 1 at the beginning of the list. The resulting list is "1 2 3 4". The original list with just 2, 3, and 4 is preserved.

Inserting at the End

To append elements to a list, use the special index "end".

linsert_end.tcl
set fruits {apple banana}
set more_fruits [linsert $fruits end cherry]
puts $more_fruits

This adds "cherry" to the end of the fruits list. The output is "apple banana cherry". Using "end" is equivalent to using the length of the list as the index.

Inserting Multiple Elements

The linsert command can insert multiple elements at once.

linsert_multiple.tcl
set letters {a b e f}
set new_letters [linsert $letters 2 c d]
puts $new_letters

This inserts both "c" and "d" at position 2 in the list. The result is "a b c d e f". Multiple elements are inserted in the order they are specified.

Inserting with Negative Indices

Negative indices count from the end of the list (-1 is the last element).

linsert_negative.tcl
set items {first second fourth}
set new_items [linsert $items -2 third]
puts $new_items

This inserts "third" before the second-to-last element ("fourth"). The output is "first second third fourth". Negative indices provide a convenient way to reference positions from the end.

Inserting into an Empty List

The linsert command can insert into an empty list at position 0.

linsert_empty.tcl
set empty_list {}
set new_list [linsert $empty_list 0 new_item]
puts $new_list

This demonstrates that linsert works with empty lists. The result is a single-element list containing "new_item". The command handles edge cases gracefully.

Best Practices

This tutorial covered the Tcl linsert command with practical examples showing its usage in different scenarios.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Tcl Tutorials.