collections

Starlark module for working with collections library.

This module implements specialized container datatypes providing alternatives to Python's general purpose built-in containers, dict, list, set, and tuple.

Similar to collections in Python.

collections.after_each(separator, iterable)

Inserts separator after each item in iterable.

Parameters:

  • separator - the value to insert after each item in iterable.

  • iterable - the list into which to intersperse the separator.

Returns: a new list with separator after each item in iterable.

collections.before_each(separator, iterable)

Inserts separator before each item in iterable.

Parameters:

  • separator - the value to insert before each item in iterable.

  • iterable - the list into which to intersperse the separator.

Returns: a new list with separator before each item in iterable.

collections.namedtuple(typename, field_names, rename=False, defaults=None, module=None)

Returns: a new subclass of tuple with named fields.

Examples:

Point = namedtuple('Point', ['x', 'y'])
Point.__doc__                # docstring for the new class
'Point(x, y)'
p = Point(11, y=22)          # instantiate with positional args or keywords
p[0] + p[1]                  # indexable like a plain tuple
33
x, y = p                     # unpack like a regular tuple
x, y
(11, 22)
p.x + p.y                    # fields also accessible by name
33
d = p._asdict()              # convert to a dictionary
d['x']
11
Point(**d)                   # convert from a dictionary
Point(x=11, y=22)
p._replace(x=100)            # _replace() is like str.replace() but targets named fields
Point(x=100, y=22)

collections.uniq(iterable)

Returns: a list of unique elements in iterable. Requires all the elements to be hashable.

Parameters:

iterable – an iterable to filter.

Returns: a new list with all unique elements from iterable.

Last updated