1. NodeBox 1
    1. Homepage
    2. NodeBox 3Node-based app for generative design and data visualization
    3. NodeBox OpenGLHardware-accelerated cross-platform graphics library
    4. NodeBox 1Generate 2D visuals using Python code (Mac OS X only)
  2. Gallery
  3. Documentation
  4. Forum
  5. Blog

Lists

Variables can hold entire lists of data. Lists are essential when your scripts increase in complexity: they are a way to group bits and pieces of information that belong together, for example: all the words to draw on screen, all the possible colors from which the script can choose, all the fonts from which the script can choose.

A list in NodeBox is no different from a list in real life: it can hold a summary of stuff to do, groceries to buy, a sheet of names and adressess. The main difference is that in real life, we usually start counting from one, while lists in programming start from zero.

The first element in a list has index zero, the second element in a list has index one.

Creating a list

So, what does the code look like? Below is the grocery list defined in NodeBox. The list is defined between square brackets. Each item in the list is a piece of text (a string), therefore they are embedded in quotes. The list is assigned to a variable named groceries, but we know that variables can have any name we like, so it could be named g or stufftodo as well for that matter.

groceries = ["apples", "pears", "carrots", "cucumber", "yoghurt", "milk"]
print len(groceries)
>>> 6

A list can contain any kind of data: strings, numbers, and even lists.
The len(list) command returns the number of items in a list.

Retrieving and editing items in a list

You can reference items in a list directly, by adding the index of the item you want to the variable name, between square brackets:

print groceries[0]
>>> apples
groceries[3] = "banana"
print groceries
>>> ["apples", "pears", "carrots", "banana", "yoghurt", "milk"]

Don't forget: items in a list start with index zero. That's why in the above example the fourth element is replaced. The fourth element has index 3.

Adding items to the list

Data in a list can change over the course of time. New items can be added to it, or old items deleted from it, just like you can add stuff to buy to a grocery sticky-note, or scratch out things.



To append an item to the end of the list:
groceries.append("eggs")
print groceries
>>> ["apples", "pears", 
>>>  "carrots", "banana", 
>>>  "yoghurt", "milk",
>>>  "eggs"]
To remove the eggs from the list:
del groceries[6]

Items can be inserted halfway the list with the list.insert(i, item) command, where the i parameter is the position where you want the element to be.

Looping through the list

Looping through a list