Skip to main content

toset Function

toset converts its argument to a set value.

Explicit type conversions are rarely necessary in OpenTofu because it will convert types automatically where required. Use the explicit type conversion functions only to normalize types returned in module outputs.

Pass a list value to toset to convert it to a set, which will remove any duplicate elements and discard the ordering of the elements.

Examples

Code Block
> toset(["a", "b", "c"])
[
"a",
"b",
"c",
]

Since OpenTofu's concept of a set requires all of the elements to be of the same type, mixed-typed elements will be converted to the most general type:

Code Block
> toset(["a", "b", 3])
[
"3",
"a",
"b",
]

Set collections are unordered and cannot contain duplicate values, so the ordering of the argument elements is lost and any duplicate values are coalesced:

Code Block
> toset(["c", "b", "b"])
[
"b",
"c",
]