A class and utilities for encoding variable objects.

The Variables class is intended to be used as an attribute of other classes, such as DiscreteQuadraticModel and SampleSet.

The requirements for the class are:
  • Have a minimal memory footprint when the variables are labeled [0, n)

  • Behave like a list for iteration and getting items

  • Behave like a set for determining if it contains an item

  • Constant time for finding the index of an item

Variables Class

class Variables[source]

Set-like and list-like variables tracking.


iterable (iterable) – An iterable of labels. Duplicate labels are ignored. All labels must be hashable.



Return True if the variables are labeled [0,n).



Return the index of v.


Return the number of times v appears in the variables.


Return an object that is json-serializable.

Mutation Methods

The Variables object comes with a number of semi-private methods that allow other classes to manipulate its contents. These are intended to by used by parent classes, not by the user. Modifying a Variables object that is an attribute of a class results in undefined behaviour.


Append a new variable.


Add new variables.


Remove the last variable.


Relabel the variables in-place.


Relabel the variables as integers in-place.