defaultlist

https://badge.fury.io/py/defaultlist.svg https://travis-ci.org/c0fec0de/defaultlist.svg?branch=master https://coveralls.io/repos/github/c0fec0de/defaultlist/badge.svg https://readthedocs.org/projects/defaultlist/badge/?version=1.0.0 https://codeclimate.com/github/c0fec0de/defaultlist.png https://img.shields.io/pypi/pyversions/defaultlist.svg https://landscape.io/github/c0fec0de/defaultlist/master/landscape.svg?style=flat

Usage

List extending automatically to the maximum requested length.

Added indicies are filled with None by default.

>>> l = defaultlist()
>>> l
[]
>>> l[2] = "C"
>>> l
[None, None, 'C']
>>> l[4]
>>> l
[None, None, 'C', None, None]

Slices and negative indicies are supported likewise

>>> l[1:4]
[None, 'C', None]
>>> l[-3]
'C'

Simple factory functions can be created via lambda.

>>> l = defaultlist(lambda: 'empty')
>>> l[2] = "C"
>>> l[4]
'empty'
>>> l
['empty', 'empty', 'C', 'empty', 'empty']

It is also possible to implement advanced factory functions:

>>> def inc():
...     inc.counter += 1
...     return inc.counter
>>> inc.counter = -1
>>> l = defaultlist(inc)
>>> l[2] = "C"
>>> l
[0, 1, 'C']
>>> l[4]
4
>>> l
[0, 1, 'C', 3, 4]

Please be aware that these functions are shared between shallow copies of the list.

>>> c = l[1:-1]
>>> c
[1, 'C', 3]
>>> c[5]
7
>>> c
[1, 'C', 3, 5, 6, 7]
>>> l[6]
9
>>> l
[0, 1, 'C', 3, 4, 8, 9]
class defaultlist.defaultlist(factory=None)[source]

Bases: list

List extending automatically to the maximum requested length.

Keyword Arguments:
 factory – Function called for every missing index.
copy()[source]

Return a shallow copy of the list. Equivalent to a[:].

Installation

To install the defaultlist module run:

pip install defaultlist

If you do not have write-permissions to the python installation, try:

pip install defaultlist --user