LinkedList#
- class tensordict.utils.LinkedList(*args, td: TensorDictBase | None = None, **kwargs)#
Bases:
listA list of the values of a non-tensor entry that writes item assignments back to the tensordict.
td[key]returns aLinkedListwhen the entry is aNonTensorStack, and so doestolist()withas_linked_list=True. Setting an item (values[0] = "z") writes the value to the tensordict. The methods that change the length of the list (append,extend,insert,pop,remove,clearanddel) change the list only, and unlink it from the tensordict: later item assignments no longer write back. ALinkedListcompares equal to a list with the same items. If the write to the tensordict raises, the list is unchanged.tolist()returns a plainlistcopy that is not linked to the tensordict, astolist()does.LinkedList(iterable), withouttd, builds a list that is not linked to any tensordict.Examples
>>> from tensordict import TensorDict >>> td = TensorDict(batch_size=[3]) >>> td["names"] = ["a", "b", "c"] >>> values = td["names"] >>> values[0] = "z" >>> td.get("names").tolist() ['z', 'b', 'c'] >>> values.tolist() ['z', 'b', 'c']
- extend(object: Any) None#
Extends the list with the items of
objectand unlinks it from the tensordict.
- insert(index: int, object: Any) None#
Inserts
objectbeforeindexand unlinks the list from the tensordict.
- pop(index: int = -1) Any#
Removes and returns the item at
indexand unlinks the list from the tensordict.
- remove(value: Any) None#
Removes the first item equal to
valueand unlinks the list from the tensordict.
- tolist() list#
Returns a plain
listcopy of the items, not linked to the tensordict.Nested
LinkedListitems, astd[key]returns for a non-tensor entry with more than one batch dimension, become nested lists, as withtolist(). The other items are not copied.Examples
>>> from tensordict import TensorDict >>> td = TensorDict(batch_size=[2, 2]) >>> td["names"] = [["a", "b"], ["c", "d"]] >>> td["names"].tolist() [['a', 'b'], ['c', 'd']]