listutils - list derivatives

Python’s builtin 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. listutils 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 namedutils.


alias of boltons.listutils.BarrelList

class boltons.listutils.BarrelList(iterable=None)[source]

The BarrelList is a list subtype backed by many dynamically-scaled sublists, to provide better scaling and random insertion/deletion characteristics. It is a subtype of the builtin list and 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)
>>> len(blist)
>>> len(blist.lists)  # how many underlying lists
>>> slice_idx = blist.lists[0][-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[source]
del_slice(start, stop, step=None)[source]

L.extend(iterable) – extend list by appending elements from the iterable

classmethod from_iterable(it)[source]
index(value[, start[, stop]]) → integer -- return first index of value.[source]

Raises ValueError if the value is not present.

insert(index, item)[source]

L.insert(index, object) – insert object before index

iter_slice(start, stop, step=None)[source]
pop([index]) → item -- remove and return item at index (default last).[source]

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