cobra.core.dictlist Module Contents Classes


A combined dict and list

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

Bases: list

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(self, id)[source]
_check(self, id)[source]

make sure duplicate id’s are not added. This function is called before adding in elements.


rebuild the _dict index

get_by_id(self, id)[source]

return the element with a matching id

list_attr(self, attribute)[source]

return a list of the given attribute for every object

get_by_any(self, iterable)[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(self, search_function, attribute=None)[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



>>> import cobra.test
>>> model = cobra.test.create_test_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(self, new_object)[source]

Replace an object by another with the same id.

append(self, object)[source]

append object to end

union(self, iterable)[source]

adds elements with id’s not already in the model

extend(self, iterable)[source]

extend list by appending elements from the iterable

_extend_nocheck(self, iterable)[source]

extends 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.

__sub__(self, other)[source]

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


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

__isub__(self, other)[source]

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


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

__add__(self, other)[source]

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


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

__iadd__(self, other)[source]

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


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


Helper for pickle.


gets 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__(self, state)[source]

sets internal state

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(self, id, *args)[source]

Determine the position in the list

id: A string or a Object

__contains__(self, object)[source]

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

object: str or Object

insert(self, index, object)[source]

insert object before index

pop(self, *args)[source]

remove and return item at index (default last).

add(self, x)[source]

Opposite of remove. Mirrors set.add

remove(self, x)[source]


Internal use only


reverse IN PLACE

sort(self, cmp=None, key=None, reverse=False)[source]

stable sort IN PLACE

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

__getitem__(self, i)[source]

x.__getitem__(y) <==> x[y]

__setitem__(self, i, y)[source]

Set self[key] to value.

__delitem__(self, index)[source]

Delete self[key].

__getslice__(self, i, j)[source]
__setslice__(self, i, j, y)[source]
__delslice__(self, i, j)[source]
__getattr__(self, attr)[source]

Default dir() implementation.