Lists

Scalars can only store a single value, but there are many cases where this is simply not enough. For this purpose, Python offers another type of variable that is comparable to a container: lists. Lists can contain a series of different mixed values. “Mixed” means that it is possible to store numbers and strings within a single list. Dependeing on the number of entries, a list allocates much more memory than a scalar. The important thing with lists is that entries are ordered. Each value has a fixed place that is connected to the number, representing its position. The position itself is up to you and you are free to determine the order, but once you are referring to a certain element in the list, the order should not be changed anymore! Regarding naming you should follow exactly the same rules as with scalars.

An empty list is introduced with a single statement. First, you have to find a variable name:

my_first_list = [ ]

  

If you want to fill the list with entries, it is necessary to differentiate between numbers and strings again. Strings are written within quotation marks, while numbers are just typed in as they are:

fruits = ["Pineapple","Mango","Banana","Papaya","Orange","Coconut"]
prices = [3.99 , 1.50 , 2.00 , 1.20 , 0.49 , 2.60]

 

To achieve direct access to the  elements of the list, you simply count through the entries starting with 0. It is important to keep this mind, otherwise you will always get the wrong result. To extract a single element in the list you have to use the following syntax (in this case the result is “Orange”):

fruits = ["Pineapple","Mango","Banana","Papaya","Orange","Coconut"]
my_favourite_fruit = fruits[4]
scene.message(my_favourite_fruit)

 

Python internally assigns the following values: Pineapple = 0, Mango = 1, Banana = 2, and so on. So the list’s third element is “Banana”. But it is not only possible to find elements by their index, you can also search through the list to find a certain element by its value:

fruits = ["Pineapple","Mango","Banana","Papaya","Orange","Coconut"]
my_favourite_fruit = "Papaya"
 
if (my_favourite_fruit in fruits):
    scene.message("Your favourite fruit is in stock")
else:
    scene.message("Sorry. Your favourite fruit is not available")

 

A list is not a static structure that is fixed, once it is created. You can append and delete certain entries with simple instructions:

fruits = ["Pineapple","Mango","Banana","Papaya","Orange","Coconut"]
new_fruit = "Kiwi"
fruits.append(new_fruit)

 

And the code for deleting an entry:

fruits = ["Pineapple","Mango","Banana","Papaya","Orange","Coconut"]
out_of_stock = "Mango"
fruits.remove(out_of_stock)

 

Something that is often used with Python and RealFlow is the number of elements within a list. In many cases you have to check whether or not there are already list entries to execute a certain function, and Python has a solution for this. Here the result is 6:

fruits = ["Pineapple","Mango","Banana","Papaya","Orange","Coconut"]
number_of_entries = len(fruits)
scene.message(str(number_of_entries))

 

You can see a new instruction in the example above: “str”. This operation translates the numerical value into a string, so it can be printed to the message window. When there are numbers inside a list, it is possible to extract certain values, assign them as variables, and perform calculations with them – the result is 4.6:

prices = [3.99 , 1.50 , 2.00 , 0.49 , 1.20 , 2.60]
price_a = prices[2]
price_b = prices[5]
price_total = price_a + price_b
scene.message(str(price_total))