list is a very fast and efficient
sequence type, but it could be better for certain access patterns,
such as non-sequential insertion into a large lists.
provides a pure-Python solution to this problem.
For utilities for working with iterables and lists, check out
iterutils. For the a
list-based version of
collections.namedtuple, check out
listsubtype backed by many dynamically-scaled sublists, to provide better scaling and random insertion/deletion characteristics. It is a subtype of the builtin
listand has an identical API, supporting indexing, slicing, sorting, etc. If application requirements call for something more performant, consider the blist module available on PyPI.
The name comes by way of Kurt Rose, who said it reminded him of barrel shifters. Not sure how, but it’s BList-like, so the name stuck. BList is of course a reference to B-trees.
Parameters: iterable – An optional iterable of initial values for the list.
>>> blist = BList(xrange(100000)) >>> blist.pop(50000) 50000 >>> len(blist) 99999 >>> len(blist.lists) # how many underlying lists 8 >>> slice_idx = blist.lists[-1] >>> blist[slice_idx:slice_idx + 2] BarrelList([11637, 11638])
Slicing is supported and works just fine across list borders, returning another instance of the BarrelList.
L.append(object) – append object to end
count(value) → integer -- return number of occurrences of value¶
del_slice(start, stop, step=None)¶
L.extend(iterable) – extend list by appending elements from the iterable
index(value[, start[, stop]]) → integer -- return first index of value.¶
Raises ValueError if the value is not present.
L.insert(index, object) – insert object before index
iter_slice(start, stop, step=None)¶
pop([index]) → item -- remove and return item at index (default last).¶
Raises IndexError if list is empty or index is out of range.
L.reverse() – reverse IN PLACE
L.sort(cmp=None, key=None, reverse=False) – stable sort IN PLACE; cmp(x, y) -> -1, 0, 1