.. note::
    :class: sphx-glr-download-link-note

    Click :ref:`here <sphx_glr_download_auto_examples_metadata_processing_plot_sequences.py>` to download the full example code
.. rst-class:: sphx-glr-example-title

.. _sphx_glr_auto_examples_metadata_processing_plot_sequences.py:


======================
Working with sequences
======================

This examples illustrates how to work with sequences.






.. rst-class:: sphx-glr-script-out

 Out:

 .. code-block:: none

    Number of blocks: [(300a, 00f8) Block Type                          CS: 'APERTURE'
    (300a, 00fe) Block Name                          LO: 'Block1'(300a, 00f8) Block Type                          CS: 'APERTURE'
    (300a, 00fe) Block Name                          LO: 'Block2']




|


.. code-block:: python


    from pydicom.sequence import Sequence
    from pydicom.dataset import Dataset

    # create to toy datasets
    block_ds1 = Dataset()
    block_ds1.BlockType = "APERTURE"
    block_ds1.BlockName = "Block1"

    block_ds2 = Dataset()
    block_ds2.BlockType = "APERTURE"
    block_ds2.BlockName = "Block2"

    beam = Dataset()
    # note that you should add beam data elements like BeamName, etc; these are
    # skipped in this example
    plan_ds = Dataset()
    # starting from scratch since we did not read a file
    plan_ds.BeamSequence = Sequence([beam])
    plan_ds.BeamSequence[0].BlockSequence = Sequence([block_ds1, block_ds2])
    plan_ds.BeamSequence[0].NumberOfBlocks = 2

    beam0 = plan_ds.BeamSequence[0]
    print('Number of blocks: {}'.format(beam0.BlockSequence))

    # create a new data set
    block_ds3 = Dataset()
    # add data elements to it as above and don't forget to update Number of Blocks
    # data element
    beam0.BlockSequence.append(block_ds3)
    del plan_ds.BeamSequence[0].BlockSequence[1]

**Total running time of the script:** ( 0 minutes  0.001 seconds)


.. _sphx_glr_download_auto_examples_metadata_processing_plot_sequences.py:


.. only :: html

 .. container:: sphx-glr-footer
    :class: sphx-glr-footer-example



  .. container:: sphx-glr-download

     :download:`Download Python source code: plot_sequences.py <plot_sequences.py>`



  .. container:: sphx-glr-download

     :download:`Download Jupyter notebook: plot_sequences.ipynb <plot_sequences.ipynb>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.readthedocs.io>`_
