On this page
collections.UserList
Prev: collections.UserDict | Up: collections | Next: collections.UserString
Rel: collections.ChainMap, collections.Counter, collections.defaultdict, collections.deque
Ref: docs.python.org — collections.UserList
Tags: #public #python3 #stdlib #collections
Import
import collections
from collections import UserList
Signature & Overview
collections.UserList(initlist=None)
A more or less complete user-defined wrapper around list objects.
Methods & Attributes
UserList.append(self, item)—UserList.appendmethod/property.UserList.clear(self)—UserList.clearmethod/property.UserList.copy(self)—UserList.copymethod/property.UserList.count(self, item)—UserList.countmethod/property.UserList.extend(self, other)—UserList.extendmethod/property.UserList.index(self, item, *args)—UserList.indexmethod/property.UserList.insert(self, i, item)—UserList.insertmethod/property.UserList.pop(self, i=-1)—UserList.popmethod/property.UserList.remove(self, item)—UserList.removemethod/property.UserList.reverse(self)—UserList.reversemethod/property.UserList.sort(self, /, *args, **kwds)—UserList.sortmethod/property.
Usage
from collections import UserList
class UniqueList(UserList):
def append(self, item) -> None:
if item not in self.data:
super().append(item)
u = UniqueList([1, 2])
u.append(2)
u.append(3)
assert list(u) == [1, 2, 3]
Gotchas
- Backing storage lives in
self.data: Operations like slicing return a new instance of yourUserListsubclass automatically. - Not a
listsubclass:isinstance(u, list)isFalse; type-check againstcollections.abc.MutableSequence.
REPL Inspect
from collections import UserList
help(UserList)
Linked from
Related notes
- tuts/python3/stdlib_module/collections/UserDict Linked here; links to this page; shared tags: #collections, #stdlib
- tuts/python3/stdlib_module/collections/UserString Linked here; links to this page; shared tags: #collections, #stdlib
- tuts/python3/stdlib_module/collections Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/collections/ChainMap Linked here; shared tags: #collections, #stdlib
- tuts/python3/stdlib_module/collections/Counter Linked here; shared tags: #collections, #stdlib
- tuts/python3/stdlib_module/collections/defaultdict Linked here; shared tags: #collections, #stdlib
- tuts/python3/stdlib_module/collections/deque Linked here; shared tags: #collections, #stdlib
- tuts/python3/stdlib_module/collections/namedtuple shared tags: #collections, #stdlib
- tuts/python3/stdlib_module/collections/OrderedDict shared tags: #collections, #stdlib
- tuts/rust/std/collections shared tags: #collections, #stdlib