Dictionaries

Dictionaries are another way to store multiple values, but they work differently from lists. With dictionaries you always store a key-value pair. This makes it possible to store the elements without a certain order. In other programming languages this structure is also called hash or associative array. Inside RealFlow, dictionaries are rather rarely used, but there might be some applications where you have to use them – for example when you want to find a certain object by its Id. To identify an element it is no longer necessary to find a certain index, it is enough to identify them with the key:

fruits = {"Pineapple":3.99,"Mango":1.50,"Orange":0.49,"Coconut":2.60}

 

To find a price for a certain fruit you can use this instruction (the result is 2.6):

fruits = {"Pineapple":3.99,"Mango":1.50,"Orange":0.49,"Coconut":2.60}
price = fruits["Coconut"]
scene.message(str(price))

 

It is also possible to append elements to a dictionary:

fruits = {"Pineapple":3.99,"Mango":1.50,"Orange":0.49,"Coconut":2.60}
new_fruit = "Banana"
new_price = 2.00
fruits[new_fruit] = new_price

 

With the len(variable) statement it is again possible to read out the number of entries. Please note that this operation prints out the total number of key-value pairs, not the entire number of individual elements.

Â