Cant figure out adding values to a clear dictionary

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By protofan
Currently I try to add values to an empty dictionary to use data later. After I use this code
while counter < ammount:
	var per_name = nameslist[randi() % nameslist.size()]
	var surname = surnameslist[randi() % surnameslist.size()]
	var age = int(rand_range(20,40))
	list[counter] = counter
	list[counter].age = age
	list[counter].name = per_name
	list[counter].surname = surname
	counter += 1
print(list)

It sends back error "Invalid set index ‘age’(on base:String) with value type of ‘String’ "
In this case list is the dictionary I am using.
What is causing the error? How can I fix it?

:bust_in_silhouette: Reply From: volzhs

I presume that counter is an integer because of while counter < ammount: line.

you assigned list[counter] = counter
it should be like this after this line.

var count = 1     # if count is 1
var list = {1:1}

it’s assigned integer value(1) with key(1)
but it seems that you want to use another dictionary for the key(1)
like…

var list = {
    1:{
        "age":10
        "name":"name"
        "surname":"surname"
    }
}

but it’s already assigned as an integer.
so, you got an error.
there’s no index for integer.

if you want it, you need to make a dictionary like this.

list[counter] = {}

instead of list[counter] = counter