Deletes items of self
using a variety of indexing styles.
Examples:
Indexing into the plist
itself:
# Basic scalar indexing:
foos = plist([pdict(foo=0, bar=0), pdict(foo=1, bar=1), pdict(foo=2, bar=0)])
del foos[0]
assert (foos.aslist() ==
[dict(foo=1, bar=1), dict(foo=2, bar=0)])
# plist slice indexing:
foos = plist([pdict(foo=0, bar=0), pdict(foo=1, bar=1), pdict(foo=2, bar=0)])
del foos[:2]
assert (foos.aslist() ==
[dict(foo=2, bar=0)])
# plist int list indexing:
foos = plist([pdict(foo=0, bar=0), pdict(foo=1, bar=1), pdict(foo=2, bar=0)])
del foos[[0, 2]]
assert (foos.aslist() ==
[dict(foo=1, bar=1)])
Indexing into the elements of the plist
:
# Basic scalar indexing:
foos = plist([pdict(foo=0, bar=0), pdict(foo=1, bar=1), pdict(foo=2, bar=0)])
del foos['foo']
assert (foos.aslist() ==
[dict(bar=0), dict(bar=1), dict(bar=0)])
# tuple indexing
foos = plist([pdict(foo=0, bar=0), pdict(foo=1, bar=1), pdict(foo=2, bar=0)])
del foos[('foo', 'bar')]
assert (foos.aslist() ==
[dict(), dict(), dict()])
# list indexing
foos = plist([pdict(foo=0, bar=0), pdict(foo=1, bar=1), pdict(foo=2, bar=0)])
del foos[['foo', 'bar', 'bar']]
assert (foos.aslist() ==
[dict(bar=0), dict(foo=1), dict(foo=2)])
Indexing into the elementes of the plist
when the elements are indexed by
int
s, slice
s, or other means that confict with plist
indexing:
# Basic scalar indexing:
pl = plist[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
del pl._[0]
assert (pl.aslist() ==
[[2, 3], [5, 6], [8, 9]])
# slice indexing (note the use of the 3-argument version of slicing):
pl = plist[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
del pl._[:2:1]
assert (pl.aslist() ==
[[3], [6], [9]])
Args:
key
: The key to index by.key
can be applied toself
directly as: Alist
ofint
s: Deletes fromself
using thoseint
s as indices. Aslice
: Deletes fromself
based on theslice
. Anint
: Deletes the value at that index.key
can be applied to elements ofself
individually: A genericlist
: Deletes from the elements ofself
using the elements ofkey
in order on the elements ofself
. Atuple
when the elements ofself
can be indexed bytuple
: Deletes from the elements ofself
by applying thattuple
to each element ofself
. Atuple
, otherwise: Deletes from the elements ofself
where each element gets each element in thekey
tuple
deleted. E.g.,del foo[('bar', 'baz')]
deletes all'bar'
and'baz'
keys from each element offoo
. Anything else: Deleteskey
from each of its elements.
Returns:
self
, in order to allow chaining throughplist.__delitem__(key)
.
Raises:
TypeError
: Ifkey
fails to be applied directly toself
and fails to be applied to its elements individually.