Support Topics Documentation Slack YouTube Blog

Appending objects to a list: Bug?

I see a behavior which seems to be a bug for me:
In UI Builder, I’m using the following codeless piece to create dropdown list options.

The print statements give:
image

Note, that final print result a list with two times “Daffy Duck”, instead having “Bug Bunny” as one of the entries.
Is image not a proper way to append entries to a list?
I also tried image instead with the same result.

Is this a bug, or how to append items to a list?

Hello, @Klaas_Klever.

Could you try something like this:
image

Best regards, Nikita.

This works. The main difference is, that there is no intermediate variable “items”.
I’ll take this, but it is unclear why my orginal approach is not working …
Regards,

This is because you are working with reference types, and when reassigning lists, you are assigning a reference, not a value. Here’s what’s going in your code:

  1. Variables are initialized.
  2. The variable “items” is assigned the value of the variable j.
  3. The variable “list” reference to the same link as items.
    At this step you have:

items [‘value’] = “Bugs Bunny”,
list [0] = “Bugs Bunny”.

The place where you are having problems is the next iteration:
4. As soon as you assign a new value to the “items” variable, your “list” variable (which points to the same place in memory) will have the same value.
5. You are assigning “items” to the “list” variable one more time.
This way you get duplicate values.

For clarity, you can try adding another print after you assigned a value to the variable “items”, but BEFORE assigning a value to the “list”.

Best regards, Nikita.

Thanks a lot! This makes it clear. Take care of references
Regards