kcl-lang / kcl-lang.io

KCL Website and Documentation Repo: https://kcl-lang.io
https://kcl-lang.github.io
Apache License 2.0
14 stars 34 forks source link

[FAQ] add a key and value to existing dict in kcl #297

Closed wilsonwang371 closed 8 months ago

wilsonwang371 commented 8 months ago

General Question

How do i add a key and value to existing dict in kcl?

For example: I want to be able to do something like this in kcl

mydict = {
key1 = "1"
key2= "2"
}
mydict[key] = value
Peefy commented 8 months ago

For example, you can use the attribute operator and union operation to do this.

mydict = {
    key1 = "1"
    key2= "2"
}  | {
    key = "value"
}

Or the unpack operator

mydict = {
    key1 = "1"
    key2= "2"
} 
newDict = {
    **mydict
    key = "value"
}

See more documents here: https://kcl-lang.io/docs/user_docs/support/faq-kcl#4-how-to-add-elements-to-a-dict

Peefy commented 8 months ago

Additionally, thank you very much for opening the issue for discussion. We will regularly synchronize user questions to the FAQ document.