This language bar is your friend. Select your favorite languages!
Select your favorite languages :
- Or search :
Idiom #29 Remove item from list, by its index
Remove i-th item from list items.
This will alter the original list or return a new list, depending on which is more idiomatic.
Note that in most languages, the smallest valid value for i is 0.
- Ada
- Clojure
- C++
- C#
- D
- Dart
- Elixir
- Erlang
- Fortran
- Go
- Go
- Go
- Haskell
- JS
- JS
- Java
- Java
- Kotlin
- Lisp
- Lua
- Obj-C
- PHP
- Pascal
- Perl
- Python
- Python
- Ruby
- Rust
- Scala
- Scheme
- Smalltalk
copy(items[i:], items[i+1:])
items[len(items)-1] = nil
items = items[:len(items)-1]
This code is for pointer value type, and has no memory leak.
items.remove(i);
Original list is altered.
Be careful not to confuse remove(int) with remove(Object), in case you have a List<Integer> (conversions can be tricky)
Be careful not to confuse remove(int) with remove(Object), in case you have a List<Integer> (conversions can be tricky)
(defun remove-item-by-index (index items)
"Construct a fresh list which is a copy of ITEMS (a list) excluding the item at INDEX."
(loop for item in items
as i from 0
unless (= index i) collect item))
Lisp lists are indexed from 0. Within the LOOP, ITEM is the current item, I is the index of that item, and the two are stepped in parallel.
programming-idioms.org