Define the DictList class.

Module Contents



Define a combined dict and list.

class cobra.core.dictlist.DictList(*args)[source]

Bases: list

Define a combined dict and list.

This object behaves like a list, but has the O(1) speed benefits of a dict when looking up elements by their id.

has_id(id: Union[cobra.core.object.Object, str]) bool[source]

Check if id is in DictList.

_check(id: Union[cobra.core.object.Object, str]) None[source]

Make sure duplicate id’s are not added.

This function is called before adding in elements.

_generate_index() None[source]

Rebuild the _dict index.

get_by_id(id: Union[cobra.core.object.Object, str]) cobra.core.object.Object[source]

Return the element with a matching id.

list_attr(attribute: str) list[source]

Return a list of the given attribute for every object.

get_by_any(iterable: List[Union[str, cobra.core.object.Object, int]]) list[source]

Get a list of members using several different ways of indexing.


iterable (list (if not, turned into single element list)) – list where each element is either int (referring to an index in in this DictList), string (a id of a member in this DictList) or member of this DictList for pass-through


a list of members

Return type


query(search_function: Union[str, Pattern, Callable], attribute: Union[str, None] = None) DictList[source]

Query the list.

  • search_function (a string, regular expression or function) – Used to find the matching elements in the list. - a regular expression (possibly compiled), in which case the given attribute of the object should match the regular expression. - a function which takes one argument and returns True for desired values

  • attribute (string or None) – the name attribute of the object to passed as argument to the search_function. If this is None, the object itself is used.


a new list of objects which match the query

Return type



>>> from import load_model
>>> model = load_model('textbook')
>>> model.reactions.query(lambda x: x.boundary)
>>> import re
>>> regex = re.compile('^g', flags=re.IGNORECASE)
>>> model.metabolites.query(regex, attribute='name')
_replace_on_id(new_object: cobra.core.object.Object) None[source]

Replace an object by another with the same id.

append(entity: cobra.core.object.Object) None[source]

Append object to end.

union(iterable: Iterable[cobra.core.object.Object]) None[source]

Add elements with id’s not already in the model.

extend(iterable: Iterable[cobra.core.object.Object]) None[source]

Extend list by appending elements from the iterable.

Sometimes during initialization from an older pickle, _dict will not have initialized yet, because the initialization class was left unspecified. This is an issue because unpickling calls DictList.extend, which requires the presence of _dict. Therefore, the issue is caught and addressed here.


iterable (Iterable) –

_extend_nocheck(iterable: Iterable[cobra.core.object.Object]) None[source]

Extend without checking for uniqueness.

This function should only be used internally by DictList when it can guarantee elements are already unique (as in when coming from self or other DictList). It will be faster because it skips these checks.


iterable (Iterable) –

__sub__(other: Iterable[cobra.core.object.Object]) DictList[source]

Remove a value or values, and returns the new DictList.

x.__sub__(y) <==> x - y


other (iterable) – other must contain only unique id’s present in the list


total – new DictList with item(s) removed

Return type


__isub__(other: Iterable[cobra.core.object.Object]) DictList[source]

Remove a value or values in place.

x.__sub__(y) <==> x -= y


other (iterable) – other must contain only unique id’s present in the list

__add__(other: Iterable[cobra.core.object.Object]) DictList[source]

Add item while returning a new DictList.

x.__add__(y) <==> x + y


other (iterable) – other must contain only unique id’s which do not intersect with self

__iadd__(other: Iterable[cobra.core.object.Object]) DictList[source]

Add item while returning the same DictList.

x.__iadd__(y) <==> x += y


other (iterable) – other must contain only unique id’s whcih do not intersect with self

__reduce__() Tuple[Type[DictList], Tuple, dict, Iterator][source]

Return a reduced version of DictList.

This reduced version details the class, an empty Tuple, a dictionary of the state and an iterator to go over the DictList.

__getstate__() dict[source]

Get internal state.

This is only provided for backwards compatibility so older versions of cobrapy can load pickles generated with cobrapy. In reality, the “_dict” state is ignored when loading a pickle

__setstate__(state: dict) None[source]

Pretend to set internal state. Actually recalculates.

Ignore the passed in state and recalculate it. This is only for compatibility with older pickles which did not correctly specify the initialization class

index(id: Union[str, cobra.core.object.Object], *args) int[source]

Determine the position in the list.


id (A string or a Object) –

__contains__(entity: Union[str, cobra.core.object.Object]) bool[source]

Ask if the DictList contain an entity.

DictList.__contains__(entity) <==> entity in DictList


entity (str or Object) –

__copy__() DictList[source]

Copy the DictList into a new one.

insert(index: int, entity: cobra.core.object.Object) None[source]

Insert entity before index.

pop(*args) cobra.core.object.Object[source]

Remove and return item at index (default last).

add(x: cobra.core.object.Object) None[source]

Opposite of remove. Mirrors set.add.

remove(x: Union[str, cobra.core.object.Object]) None[source]


Internal use only.

Each item is unique in the list which allows this It is much faster to do a dict lookup than n string comparisons

reverse() None[source]

Reverse IN PLACE.

sort(cmp: Callable = None, key: Callable = None, reverse: bool = False) None[source]

Stable sort IN PLACE.

cmp(x, y) -> -1, 0, 1

__getitem__(i: Union[int, slice, Iterable, cobra.core.object.Object, DictList]) Union[DictList, cobra.core.object.Object][source]

Get item from DictList.

__setitem__(i: Union[slice, int], y: Union[list, cobra.core.object.Object]) None[source]

Set an item via index or slice.

  • i (slice, int) – i can be slice or int. If i is a slice, y needs to be a list

  • y (list, Object) – Object to set as

__delitem__(index: int) None[source]

Remove item from DictList.

__getslice__(i: int, j: int) DictList[source]

Get a slice from it to j of DictList.

__setslice__(i: int, j: int, y: Union[list, cobra.core.object.Object]) None[source]

Set slice, where y is an iterable.

__delslice__(i: int, j: int) None[source]

Remove slice.

__getattr__(attr: Any) Any[source]

Get an attribute by id.

__dir__() list[source]

Directory of the DictList.

Override this to allow tab complete of items by their id.


attributes – A list of attributes/entities.

Return type