Delete element

2 snippets across 2 stacks — JavaScript, Python

Also written as remove element

JSJavaScript

Removing Elements

JS · DOM Manipulation
syntax
element.remove()
parent.removeChild(child)
example
// Modern: element removes itself
const notification = document.querySelector(".notification");
notification.remove();

// Remove all children
const container = document.querySelector("#list");
while (container.firstChild) {
  container.removeChild(container.firstChild);
}

// Faster: clear all children
container.replaceChildren();

Note el.remove() is cleaner than parentNode.removeChild(el). Use replaceChildren() with no arguments to efficiently clear all children.

PYPython

remove, pop, del, clear

PY · Lists
syntax
list.remove(value)
list.pop(index)
del list[index]
example
items = ["a", "b", "c", "d", "e"]
items.remove("c")
print(items)
last = items.pop()
print(last, items)
del items[0]
print(items)
output
['a', 'b', 'd', 'e']
e ['a', 'b', 'd']
['b', 'd']

Note remove() deletes the first matching value (raises ValueError if missing). pop() removes by index and returns the value. del removes by index without returning.