Containers, basic operations onΒΆ

Abjad containers work like Python lists.


Container properties

The length of a container is defined equal to the number of components in the container:

>>> container = abjad.Container("ds'16 cs'16 e'16 c'16 d'2 ~ d'8")
>>> abjad.show(container)
>>> len(container)
6

Iteration

Containers iterate their contents like lists:

>>> for item in container:
...     item
... 
Note("ds'16")
Note("cs'16")
Note("e'16")
Note("c'16")
Note("d'2")
Note("d'8")

Item getting

Item getting returns one component at a time:

>>> container[0]
Note("ds'16")
>>> container[1]
Note("cs'16")
>>> container[2]
Note("e'16")

Slicing returns a selection of components:

>>> container[:3]
[Note("ds'16"), Note("cs'16"), Note("e'16")]

Containment testing

Containment testing and indexing work like lists:

>>> note = container[0]
>>> note in container
True
>>> container.index(note)
0
>>> rest = abjad.Rest("r4")
>>> rest in container
False

Adding components

Append, extend and insert work like lists:

>>> container.append("af'32")
>>> abjad.show(container)
>>> container.extend("c''32 a'32")
>>> abjad.show(container)
>>> container.insert(-3, "g'32")
>>> abjad.show(container)

Removing components

And pop and remove work like lists, too:

>>> note = container.pop(-1)
>>> abjad.show(container)
>>> note = container[-1]
>>> container.remove(note)
>>> abjad.show(container)