6.3 Lists

List objects hold ordered sequences of other Pyxplot objects, which may include lists and dictionaries to create hierarchical data structures. They are created by enclosing a comma-separated list of objects by square brackets.

For example:

a = [10,colors.green,"bottles"]

Once created, more items can be added to a list using its append(item) and insert(n,item) methods, where the latter inserts an item at position $n$:

pyxplot> theFive = ["Cui","Mussorgsky"]
pyxplot> theFive.append("Borodin")
["Cui", "Mussorgsky", "Borodin"]
pyxplot> theFive.insert(0,"Balakirev")
["Balakirev", "Cui", "Mussorgsky", "Borodin"]
pyxplot> theFive.insert(-2,"Rimsky-Korsakov")
["Balakirev", "Cui", "Mussorgsky", "Rimsky-Korsakov", "Borodin"]

A complete list of the methods available on lists (itself a list of strings) can be found by calling the method [].methods(); they are also listed in Section 13.10. As with string methods, documentation of list methods is returned if the method object is printed:

pyxplot> print [].append
append(x) appends the object x to a list.
pyxplot> print [].insert
insert(n,x) inserts the object x into a list at position n.

Most methods that operate on lists, for example, append, extend and sort operations, return the list as their output. Unless this is stored in a variable, Pyxplot prints this return value to the terminal. In some cases this is useful: in the example above, it allowed us to see how the list was changing when we called its append() and insert() methods. Often, however, this terminal spam is unwanted. The call command allows methods to be called without printing their output, which is discarded:

pyxplot> a = ["William I"]
pyxplot> call a.extend(["William II","Henry I"])
pyxplot> call a.insert(0,"Edgar II")
pyxplot> call a.insert(0,"Edward the Confessor")
pyxplot> print a
["Edward the Confessor", "Edgar II", "William I", "William II", "Henry I"]