cobra.core.dictlist Module Contents

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

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.


Instantiate a combined dict and list.

Parameters:args (iterable) – iterable as single argument to create new DictList from

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


rebuild the _dict index


return the element with a matching id


return a list of the given attribute for every object


Get a list of members using several different ways of indexing

Parameters: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
Returns:a list of members
Return type:list
query(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 an object by another with the same id.


append object to end


adds elements with id’s not already in the model


extend list by appending elements from the iterable


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.


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

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

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

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

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

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

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

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

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


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

Determine the position in the list

id: A string or a Object


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

object: str or Object

insert(index, object)[source]

insert object before index


remove and return item at index (default last).


Opposite of remove. Mirrors set.add



Internal use only


reverse IN PLACE

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

stable sort IN PLACE

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

__setitem__(i, y)[source]
__getslice__(i, j)[source]
__setslice__(i, j, y)[source]
__delslice__(i, j)[source]