nustack

collections.views

Dict view interfaces - DictKeys, DictValues, DictItems.

Module nu.forms.collections.views.

Dict view interfaces - DictKeys, DictValues, DictItems.

NameSortCallMeaning
DictItemsscalar_queryDictItems()View of a Dict's (key, value) pairs, produced by dict.items().
DictKeysscalar_queryDictKeys()View of a Dict's keys, produced by dict.keys().
DictValuesscalar_queryDictValues()View of a Dict's values, produced by dict.values().

DictItems

View of a Dict's (key, value) pairs, produced by dict.items().

DictItems()

Path nu.forms.DictItems. Kind ScalarQuery, sort scalar_query, cardinality scalar.

Notes

  • Set-like: union, intersection, difference, issubset, and the | & - ^ operators all work on it directly, matching Python's dict.items() when the values are hashable.
  • Lazy and live: it holds no items of its own, it re-reads the backing Dict on every evaluation. Mutate the Dict and the view reflects it on the next nu.run.
  • Iterable, sized (len()), and supports contains with a (key, value) tuple.

Example

items = nu.Dict({"a": 1, "b": 2}).items()
nu.run(items.len())[0]
nu.run(items.contains(("a", 1)))[0]
2
True

Methods

.to_list()

Snapshot the items into a List.

Builds List[tuple[K, V]].

Yields

The (key, value) pairs as a plain List, in dict iteration order. Unlike the view itself, the result no longer tracks the backing Dict.

Example

nu.run(nu.Dict({"a": 1, "b": 2}).items().to_list())[0]
[('a', 1), ('b', 2)]

.to_set()

Snapshot the items into a Set.

Builds Set[tuple[K, V]].

Yields

The (key, value) pairs as a plain Set. Unlike the view itself, the result no longer tracks the backing Dict.

Notes

  • Raises at evaluation time if any value is unhashable.

Example

nu.run(nu.Dict({"a": 1, "b": 2}).items().to_set())[0] == {("a", 1), ("b", 2)}
True

Inherited methods

From nu.forms.collections.abc.set_.SetLikeForm:

CallBuildsMeaning
.union(other)CollectionResultTUnion of self and other.
.intersection(other)CollectionResultTIntersection of self and other.
.difference(other)CollectionResultTElements of self that are not in other.
.symmetric_difference(other)CollectionResultTElements in exactly one of self and other, not both.
.issubset(other)BoolWhether every element of self is in other.
.issuperset(other)BoolWhether every element of other is in self.
.isdisjoint(other)BoolWhether self and other share no elements.
.copy()CollectionResultTShallow copy of self.

From nu.forms.collections.abc.collection.CollectionForm:

CallBuildsMeaning
.extract()objectMaterialise the full subtree rooted at self.

From nu.forms.collections.abc.sized.SizedForm:

CallBuildsMeaning
.len()IntLength of self.

From nu.forms.collections.abc.iterable.IterableForm:

CallBuildsMeaning
.iter()Iterator[ElementT]Open self into a lazy iterator stream over its elements.

From nu.forms.collections.abc.container.ContainerForm:

CallBuildsMeaning
.contains(item)BoolWhether item is a member of self.

From nu.lang.forms.Form:

CallBuildsMeaning
a == bBoolSelf equal to other by value: an Eq term, never a Python bool.
a != bBoolSelf not equal to other by value: a Ne term, never a Python bool.
.is_empty()BoolTrue if this Form yields the EMPTY sentinel.
.not_empty()BoolTrue if this Form does not yield EMPTY.
.fallback(*alternatives)SelfThe first of this value and alternatives that is not EMPTY.

DictKeys

View of a Dict's keys, produced by dict.keys().

DictKeys()

Path nu.forms.DictKeys. Kind ScalarQuery, sort scalar_query, cardinality scalar.

Notes

  • Set-like: union, intersection, difference, issubset, and the | & - ^ operators all work on it directly, matching Python's dict.keys().
  • Lazy and live: it holds no keys of its own, it re-reads the backing Dict on every evaluation. Mutate the Dict and the view reflects it on the next nu.run.
  • Iterable, sized (len()), and supports contains.

Example

keys = nu.Dict({"a": 1, "b": 2}).keys()
nu.run(keys.len())[0]
nu.run(keys.contains("a"))[0]
2
True

Methods

.to_list()

Snapshot the keys into a List.

Builds List[K].

Yields

The keys as a plain List, in dict iteration order. Unlike the view itself, the result no longer tracks the backing Dict.

Example

nu.run(nu.Dict({"a": 1, "b": 2}).keys().to_list())[0]
['a', 'b']

.to_set()

Snapshot the keys into a Set.

Builds Set[K].

Yields

The keys as a plain Set. Unlike the view itself, the result no longer tracks the backing Dict.

Example

nu.run(nu.Dict({"a": 1, "b": 2}).keys().to_set())[0] == {"a", "b"}
True

Inherited methods

From nu.forms.collections.abc.set_.SetLikeForm:

CallBuildsMeaning
.union(other)CollectionResultTUnion of self and other.
.intersection(other)CollectionResultTIntersection of self and other.
.difference(other)CollectionResultTElements of self that are not in other.
.symmetric_difference(other)CollectionResultTElements in exactly one of self and other, not both.
.issubset(other)BoolWhether every element of self is in other.
.issuperset(other)BoolWhether every element of other is in self.
.isdisjoint(other)BoolWhether self and other share no elements.
.copy()CollectionResultTShallow copy of self.

From nu.forms.collections.abc.collection.CollectionForm:

CallBuildsMeaning
.extract()objectMaterialise the full subtree rooted at self.

From nu.forms.collections.abc.sized.SizedForm:

CallBuildsMeaning
.len()IntLength of self.

From nu.forms.collections.abc.iterable.IterableForm:

CallBuildsMeaning
.iter()Iterator[ElementT]Open self into a lazy iterator stream over its elements.

From nu.forms.collections.abc.container.ContainerForm:

CallBuildsMeaning
.contains(item)BoolWhether item is a member of self.

From nu.lang.forms.Form:

CallBuildsMeaning
a == bBoolSelf equal to other by value: an Eq term, never a Python bool.
a != bBoolSelf not equal to other by value: a Ne term, never a Python bool.
.is_empty()BoolTrue if this Form yields the EMPTY sentinel.
.not_empty()BoolTrue if this Form does not yield EMPTY.
.fallback(*alternatives)SelfThe first of this value and alternatives that is not EMPTY.

DictValues

View of a Dict's values, produced by dict.values().

DictValues()

Path nu.forms.DictValues. Kind ScalarQuery, sort scalar_query, cardinality scalar.

Notes

  • Not set-like: values can repeat and aren't required to be hashable, so there's no union/intersection here, unlike DictKeys and DictItems. Just Collection: iterable, sized (len()), and contains.
  • Lazy and live: it holds no values of its own, it re-reads the backing Dict on every evaluation. Mutate the Dict and the view reflects it on the next nu.run.
  • No == / !=: Python compares values views by identity, never by content, so both raise. Compare a snapshot instead: values.to_list() == [...].

Example

values = nu.Dict({"a": 1, "b": 2}).values()
nu.run(values.len())[0]
nu.run(values.contains(1))[0]
2
True

Methods

.to_list()

Snapshot the values into a List.

Builds List[V].

Yields

The values as a plain List, in dict iteration order. Unlike the view itself, the result no longer tracks the backing Dict.

Example

nu.run(nu.Dict({"a": 1, "b": 2}).values().to_list())[0]
[1, 2]

.to_set()

Snapshot the values into a Set.

Builds Set[V].

Yields

The values as a plain Set. Unlike the view itself, the result no longer tracks the backing Dict.

Notes

  • Raises at evaluation time if any value is unhashable.

Example

nu.run(nu.Dict({"a": 1, "b": 2}).values().to_set())[0] == {1, 2}
True

Inherited methods

From nu.forms.collections.abc.collection.CollectionForm:

CallBuildsMeaning
.extract()objectMaterialise the full subtree rooted at self.

From nu.forms.collections.abc.sized.SizedForm:

CallBuildsMeaning
.len()IntLength of self.

From nu.forms.collections.abc.iterable.IterableForm:

CallBuildsMeaning
.iter()Iterator[ElementT]Open self into a lazy iterator stream over its elements.

From nu.forms.collections.abc.container.ContainerForm:

CallBuildsMeaning
.contains(item)BoolWhether item is a member of self.

From nu.lang.forms.Form:

CallBuildsMeaning
.is_empty()BoolTrue if this Form yields the EMPTY sentinel.
.not_empty()BoolTrue if this Form does not yield EMPTY.
.fallback(*alternatives)SelfThe first of this value and alternatives that is not EMPTY.

On this page