Write To Nested Dictionary (Kotlin)

I have declared a dictionary in Swift as such: private val nestedDictionary = mutableMapOf<String, MutableMap<String, MutableList<String>>>()

What I am trying to do now is to write to the nested dictionary. Below is the code that I am using. nestedDictionary["First Layer"]["Second Layer"] = mutableListOf("Failing to Write")

What I am trying to do is to create a dictionary key for ["First Layer"], and then map values into it. How might I do that?

Edit: The code that I have currently and is receiving this error: 'the expression cannot be a selector.'

@Simulant's answer will overwrite the existing value for "First Layer", if there is one. If that's not what you want, use getOrPut:

nestedDictionary.getOrPut("First Layer", { mutableMapOf() })["Second Layer"] = 
    mutableListOf("Failing to Write")

Python Nested Dictionary (With Examples), It contains well written, well thought and well explained computer science and Nested Dictionary: Nesting Dictionary means putting a dictionary inside another  Python – Convert Lists to Nested Dictionary Sometimes, while working with Python dictionaries, we can have a problem in which we need to convert list to nestings, i.e each list values representing new nested level.

Some dirty solution:

nestedDictionary["First Layer"]?.put("Second Layer", mutableListOf("Failing to Write"))
// Map.get() is nullable

or

nestedDictionary["First Layer"] = mutableMapOf("Second Layer" to mutableListOf("Failing to Write"))

or

nestedDictionary["First Layer"]!! += "Second Layer" to mutableListOf("Failing to Write")
// throw exception if no "First Layer" in nestedDictionary.

Python, A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and  Python Nested Dictionary In this article, you’ll learn about nested dictionary in Python. More specifically, you’ll learn to create nested dictionary, access elements, modify them and so on with the help of examples.

nestedDictionary["First Layer"] can return a not null value. So you cannot chain nestedDictionary["First Layer"]["Second Layer"] as this implies that the value stored at nestedDictionary["First Layer"] exists.

You can use the not null assertion operator to force your code execution. However you will get a KoltinNullPointerException if the value at nestedDictionary["First Layer"] was not initialized before.

val nestedDictionary = mutableMapOf<String, MutableMap<String, List<String>>>()
nestedDictionary["First Layer"]!!["Second Layer"] = mutableListOf("possible to write")

Result

Exception in thread "main" kotlin.KotlinNullPointerException
    at main(Main.kt:4)

this works because the map in between is initialized

val nestedDictionary = mutableMapOf<String, MutableMap<String, List<String>>>()
nestedDictionary["First Layer"] = HashMap()
nestedDictionary["First Layer"]!!["Second Layer"] = mutableListOf("possible to insert")

A cleaner solution would be

val nestedDictionary = mutableMapOf<String, MutableMap<String, MutableList<String>>>()
nestedDictionary["First Layer"] = mutableMapOf("Second Layer" to mutableListOf("inserted"))

Python, Nested Classes · Enum Classes · Objects · Type Aliases · Inline Classes Collection Write Operations · List Specific Operations · Set Specific Operations. Method #2 : Using update() This method is used in cases where more than one keys need to be added to the nested dictionaries. The update function accepts the dictionary and added the dictionary with the keys in it.

hashMapOf, Nested Classes · Enum Classes · Objects · Type Aliases · Inline Classes Collection Write Operations · List Specific Operations · Set Specific Operations. Python – Convert Flat dictionaries to Nested dictionary Sometimes, while working with records, we can have problem in which we need to perform the task of conversion of multiple flat dictionaries to a single nested dictionary.

mapValues, Nested Classes · Enum Classes · Objects · Type Aliases · Inline Classes Collection Write Operations · List Specific Operations · Set Specific Operations. Given a nested dictionary, the task is to convert this dictionary into a flattened dictionary where the key is separated by ‘_’ in case of the nested key to be started. Given below are a few methods to solve the above task.

filterKeys, Nested Classes · Enum Classes · Objects · Type Aliases · Inline Classes Collection Write Operations · List Specific Operations · Set Specific Operations. Don't be a nana and try to encode your whole dictionary into ascii like me. – Frikster Jul 26 '17 at 4:11 If my value of any key is 0 then I got key as value like orgname1,orgname1,1,1 if 2015: 0 is for orgname1.

Comments
  • Upvoted: I love the clean solution of nestedDictionary["First Layer"]!!["Second Layer"].
  • How would I append an item to the mutableMapOf["Second Layer"]/create it if it doesn't already exist?