Flatten nested lists - Utilities

Skip to content
Back to top
View this page
Edit this page

# Flatten nested lists

sage.misc.flatten.flatten(in_list, ltypes=(<class 'list'>, <class 'tuple'>), max_level=9223372036854775807)

  Flatten a nested list.

  INPUT:

  - `in_list` – list or tuple

  - `ltypes` – (optional) list of particular types to flatten

  - `max_level` – the maximum level to flatten

  OUTPUT: a flat list of the entries of `in_list`

  EXAMPLES:

  Sage

      sage: flatten([[1,1],[1],2])
      [1, 1, 1, 2]
      sage: flatten([[1,2,3], (4,5), [[[1],[2]]]])
      [1, 2, 3, 4, 5, 1, 2]
      sage: flatten([[1,2,3], (4,5), [[[1],[2]]]], max_level=1)
      [1, 2, 3, 4, 5, [[1], [2]]]
      sage: flatten([[[3],[]]],max_level=0)
      [[[3], []]]
      sage: flatten([[[3],[]]],max_level=1)
      [[3], []]
      sage: flatten([[[3],[]]],max_level=2)
      [3]

  Python

      >>> from sage.all import *
      >>> flatten([[Integer(1),Integer(1)],[Integer(1)],Integer(2)])
      [1, 1, 1, 2]
      >>> flatten([[Integer(1),Integer(2),Integer(3)], (Integer(4),Integer(5)), [[[Integer(1)],[Integer(2)]]]])
      [1, 2, 3, 4, 5, 1, 2]
      >>> flatten([[Integer(1),Integer(2),Integer(3)], (Integer(4),Integer(5)), [[[Integer(1)],[Integer(2)]]]], max_level=Integer(1))
      [1, 2, 3, 4, 5, [[1], [2]]]
      >>> flatten([[[Integer(3)],[]]],max_level=Integer(0))
      [[[3], []]]
      >>> flatten([[[Integer(3)],[]]],max_level=Integer(1))
      [[3], []]
      >>> flatten([[[Integer(3)],[]]],max_level=Integer(2))
      [3]

  In the following example, the vector is not flattened because it is not given in the `ltypes` input.

  Sage

      sage: flatten((['Hi', 2, vector(QQ, [1,2,3])], (4,5,6)))                         # needs sage.modules
      ['Hi', 2, (1, 2, 3), 4, 5, 6]

  [Python]

      >>> from sage.all import *
      >>> flatten((['Hi', Integer(2), vector(QQ, [Integer(1),Integer(2),Integer(3)])], (Integer(4),Integer(5),Integer(6))))                         # needs sage.modules
      ['Hi', 2, (1, 2, 3), 4, 5, 6]

  We give the vector type and then even the vector gets flattened:

  Sage

      sage: tV = sage.modules.vector_rational_dense.Vector_rational_dense              # needs sage.modules
      sage: flatten((['Hi', 2, vector(QQ, [1,2,3])], (4,5,6)),                         # needs sage.modules
      ....:         ltypes=(list, tuple, tV))
      ['Hi', 2, 1, 2, 3, 4, 5, 6]

  Python

      >>> from sage.all import *
      >>> tV = sage.modules.vector_rational_dense.Vector_rational_dense              # needs sage.modules
      >>> flatten((['Hi', Integer(2), vector(QQ, [Integer(1),Integer(2),Integer(3)])], (Integer(4),Integer(5),Integer(6))),                         # needs sage.modules
      ...         ltypes=(list, tuple, tV))
      ['Hi', 2, 1, 2, 3, 4, 5, 6]

  We flatten a finite field.

  Sage

      sage: flatten(GF(5))
      [0, 1, 2, 3, 4]
      sage: flatten([GF(5)])
      [Finite Field of size 5]
      sage: tGF = type(GF(5))
      sage: flatten([GF(5)], ltypes=(list, tuple, tGF))
      [0, 1, 2, 3, 4]

  [Python]

      >>> from sage.all import *
      >>> flatten(GF(Integer(5)))
      [0, 1, 2, 3, 4]
      >>> flatten([GF(Integer(5))])
      [Finite Field of size 5]
      >>> tGF = type(GF(Integer(5)))
      >>> flatten([GF(Integer(5))], ltypes=(list, tuple, tGF))
      [0, 1, 2, 3, 4]

  Degenerate cases:

  Sage

      sage: flatten([[],[]])
      []
      sage: flatten([[[]]])
      []

  Python

      >>> from sage.all import *
      >>> flatten([[],[]])
      []
      >>> flatten([[[]]])
      []