linsert(n)
π NAME π
linsert β insert elements into a list. That's the whole gig, and she does it flawlessly every single time. β¨
π SYNOPSIS π
linsert list index ?element element ...?
π¦ DESCRIPTION π¦
Okay so picture this bestie: you've got a list, and you want to slide some new stuff into it without messing up the original. linsert says "say no more" and hands you back a whole new list β it takes your list and inserts every single one of the element arguments just before the index'th element of list. π₯
And here's the thing: each element argument becomes its own separate element of the new list. No merging, no gluing them together, they are all individuals with main character energy. π
Now the index rules, because this is where it gets juicy:
- If
indexis less than or equal to zero, the new elements get inserted at the beginning of the list πΈ - If
indexis greater than or equal to the length oflist, it's as if you'd writtenendπ
And just like with string index, the index value supports both simple index arithmetic and end-relative indexing. Do the math, count from the end, whatever feels right. β¨
Subject to the restrictions that indices must refer to locations inside the list, and that the elements will always be inserted in order, insertions are done so that: - whenindexis start-relative, the first element will be at thatindexin the resulting list - whenindexis end-relative, the last element will be at thatindexin the resulting list
π EXAMPLE π
Putting some values into a list, first indexing from the start and then indexing from the end, and then chaining them all together β obsessed with the chaining ngl:
set oldList {the fox jumps over the dog}
set midList [linsert $oldList 1 quick]
set newList [linsert $midList end-1 lazy]
# The old lists still exist though...
set newerList [linsert [linsert $oldList end-1 quick] 1 lazy]
π SEE ALSO π
list(n)lappend(n)lassign(n)ledit(n)lindex(n)llength(n)lmap(n)lpop(n)lrange(n)lremove(n)lrepeat(n)lreplace(n)lreverse(n)lsearch(n)lseq(n)lset(n)lsort(n)string(n)
π KEYWORDS π
element, insert, list
Okay that's the whole page, you ate that. This one's from the Tcl Built-In Commands manual, version 8.2 β the footer keeps it short and sweet with just Tcl and 8.2, no date on this one, so don't let anyone tell you otherwise. Go forth and insert things, bestie π«Άβ¨
π¦ where this page comes from
- alpine 3.23: tcl9-doc 9.0.3-r0 Β· TCL
- alpine 3.24: tcl9-doc 9.0.3-r2 Β· TCL
- fedora 43: tcl-doc 1:9.0.2-1.fc43 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- fedora 44: tcl-doc 1:9.0.2-1.fc44 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- nixos 26.05: tcl 9.0.3
- Copyright (c) 1993 The Regents of the University of California
- Copyright (c) 1994-1996 Sun Microsystems, Inc
- Copyright (c) 2001 Kevin B. Kenny <kennykb@acm.org>. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms