Reputation: 1155
I have created a map with the following:
var names = mutableMapOf<String, Map<String, List<String>>>(
"test" to mapOf(
"first" to listOf(
"Spongebob", "Patrick"
),
"last" to listOf(
"Squarepants", "Star"
)
)
)
Next I would like to modify the map, which I think intuitively would be something like:
names["test"]["first"].add("Squidward")
However, this line is giving me an error: Expecting a top level declaration
I don't understand this; is my MutableMap definition not the declaration?
I'm coming from Python, so I'm expecting to be able to access my dict by keys and edit as necessary.
Upvotes: 0
Views: 245
Reputation: 1155
As @PiRocks pointed out in a comment, the issue was that I wasn't wrapping it all in a function.
I ultimately solved the issue by separating out the nested dict structure and creating sub classes with their own "first" and "last" list variables. I think this reduced complexity in my case and it now seems to work great.
Upvotes: 0
Reputation: 21
You are trying to modify a list which is immutable, try this:
var names = mutableMapOf<String, MutableMap<String, MutableList<String>>>(
"test" to mutableMapOf(
"first" to mutableListOf(
"Spongebob", "Patrick"
),
"last" to mutableListOf(
"Squarepants", "Star"
)
)
)
Upvotes: 1