Useful utilities for Python.
Supports Python 2.7+ and 3.2+.
The Collection class provides a fluent, convenient wrapper for working with list of data.
To instantiatte a Collection you can also use the collect() helper.
For the remainder of this documentation, we'll discuss each method available on the Collection class.
Remember, all of these methods may be chained for fluently manipulating the underlying list or dict.
Furthermore, almost every method returns a new Collection instance,
allowing you to preserve the original copy of the collection when necessary.
You may select any method from this table to see an example of its usage:
- all
- avg
- chunk
- collapse
- contains
- count
- diff
- each
- every
- filter
- first
- flatten
- forget
- for_page
- get
- implode
- is_empty
- last
- map
- merge
- pluck
- pop
- prepend
- pull
- push
- put
- reduce
- reject
- reverse
- serialize
- shift
- sort
- sum
- take
- to_json
- transform
- unique
- where
- zip
The all method simply returns the underlying list represented by the collection:
Collection([1, 2, 3]).all()
# [1, 2, 3]The avg method returns the average of all items in the collection:
Collection([1, 2, 3, 4, 5]).avg()
# 3If the collection contains nested objects or dictionaries, you must pass a key to use for determining which values to calculate the average:
collection = Collection([
{'name': 'JavaScript: The Good Parts', 'pages': 176},
{'name': 'JavaScript: The Defnitive Guide', 'pages': 1096}
])
collection.avg('pages')
# 636The chunk method breaks the collection into multiple, smaller collections of a given size:
collection = Collection([1, 2, 3, 4, 5, 6, 7])
chunks = collection.chunk(4)
chunks.serialize()
# [[1, 2, 3, 4], [5, 6, 7]]The collapse method collapses a collection of lists into a flat collection:
collection = Collection([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
collapsed = collection.collapse()
collapsed.all()
# [1, 2, 3, 4, 5, 6, 7, 8, 9]The contains method determines whether the collection contains a given item:
collection = Collection(['foo', 'bar'])
collection.contains('foo')
# TrueYou can also use the in keyword:
'foo' in collection
# TrueYou can also pass a key / value pair to the contains method,
which will determine if the given pair exists in the collection:
collection = Collection([
{'name': 'John', 'id': 1},
{'name': 'Jane', 'id': 2}
])
collection.contains('name', 'Simon')
# FalseFinally, you may also pass a callback to the contains method to perform your own truth test:
collection = Collection([1, 2, 3, 4, 5])
collection.contains(lambda item: item > 5)
# FalseThe count method returns the total number of items in the collection:
collection = Collection([1, 2, 3, 4])
collection.count()
# 4The len function can also be used:
len(collection)
# 4The diff method compares the collection against another collection, a list or a dict:
collection = Collection([1, 2, 3, 4, 5])
diff = collection.diff([2, 4, 6, 8])
diff.all()
# [1, 3, 5]The each method iterates over the items in the collection and passes each item to a given callback:
posts.each(lambda post: post.author().save(author))Return False from your callback to break out of the loop:
posts.each(lambda post: post.author().save(author) if author.name == 'John' else False)The every method creates a new collection consisting of every n-th element:
collection = Collection(['a', 'b', 'c', 'd', 'e', 'f'])
collection.every(4).all()
# ['a', 'e']You can optionally pass the offset as the second argument:
collection.every(4, 1).all()
# ['b', 'f']The filter method filters the collection by a given callback,
keeping only those items that pass a given truth test:
collection = Collection([1, 2, 3, 4])
filtered = collection.filter(lambda item: item > 2)
filtered.all()
# [3, 4]The first method returns the first element in the collection
that passes a given truth test:
collection = Collection([1, 2, 3, 4])
collection.first(lambda item: item > 2)
# 3You can also call the first method with no arguments
to get the first element in the collection.
If the collection is empty, None is returned:
collection.first()
# 1The flatten method flattens a multi-dimensional collection into a single dimension:
collection = Collection([1, 2, [3, 4, 5, {'foo': 'bar'}]])
flattened = collection.flatten()
flattened.all()
# [1, 2, 3, 4, 5, 'bar']The forget method removes an item from the collection by its key:
collection = Collection([1, 2, 3, 4, 5])
collection.forget(1)
collection.all()
# [1, 3, 4, 5]Warning
Unlike most other collection methods, forget does not return a new modified collection;
it modifies the collection it is called on.
The for_page method returns a new collection containing
the items that would be present on a given page number:
collection = Collection([1, 2, 3, 4, 5, 6, 7, 8, 9])
chunk = collection.for_page(2, 3)
chunk.all()
# 4, 5, 6The method requires the page number and the number of items to show per page, respectively.
The get method returns the item at a given key. If the key does not exist, None is returned:
collection = Collection([1, 2, 3])
collection.get(3)
# NoneYou can optionally pass a default value as the second argument:
collection = Collection([1, 2, 3])
collection.get(3, 'default-value')
# default-valueThe implode method joins the items in a collection.
Its arguments depend on the type of items in the collection.
If the collection contains dictionaries or objects, you must pass the key of the attributes you wish to join, and the "glue" string you wish to place between the values:
collection = Collection([
{'account_id': 1, 'product': 'Desk'},
{'account_id': 2, 'product': 'Chair'}
])
collection.implode('product', ', ')
# Desk, ChairIf the collection contains simple strings, simply pass the "glue" as the only argument to the method:
collection = Collection(['foo', 'bar', 'baz'])
collection.implode('-')
# foo-bar-bazThe is_empty method returns True if the collection is empty; otherwise, False is returned:
Collection([]).is_empty()
# TrueThe last method returns the last element in the collection that passes a given truth test:
collection = Collection([1, 2, 3, 4])
last = collection.last(lambda item: item < 3)
# 2You can also call the last method with no arguments to get the last element in the collection.
If the collection is empty, None is returned:
collection.last()
# 4The map method iterates through the collection and passes each value to the given callback.
The callback is free to modify the item and return it, thus forming a new collection of modified items:
collection = Collection([1, 2, 3, 4])
multiplied = collection.map(lambda item: item * 2)
multiplied.all()
# [2, 4, 6, 8]Warning
Like most other collection methods, map returns a new Collection instance;
it does not modify the collection it is called on.
If you want to transform the original collection, use the transform method.
The merge method merges the given list into the collection:
collection = Collection(['Desk', 'Chair'])
collection.merge(['Bookcase', 'Door'])
collection.all()
# ['Desk', 'Chair', 'Bookcase', 'Door']Warning
Unlike most other collection methods, merge does not return a new modified collection;
it modifies the collection it is called on.
The pluck method retrieves all of the collection values for a given key:
collection = Collection([
{'product_id': 1, 'product': 'Desk'},
{'product_id': 2, 'product': 'Chair'}
])
plucked = collection.pluck('product')
plucked.all()
# ['Desk', 'Chair']You can also specify how you wish the resulting collection to be keyed:
plucked = collection.pluck('name', 'product_id')
plucked
# {1: 'Desk', 2: 'Chair'}The pop method removes and returns the last item from the collection:
collection = Collection([1, 2, 3, 4, 5])
collection.pop()
# 5
collection.all()
# [1, 2, 3, 4]The prepend method adds an item to the beginning of the collection:
collection = Collection([1, 2, 3, 4])
collection.prepend(0)
collection.all()
# [0, 1, 2, 3, 4]The pull method removes and returns an item from the collection by its key:
collection = Collection([1, 2, 3, 4])
collection.pull(1)
collection.all()
# [1, 3, 4]The push (or append) method appends an item to the end of the collection:
collection = Collection([1, 2, 3, 4])
collection.push(5)
collection.all()
# [1, 2, 3, 4, 5]The put method sets the given key and value in the collection:
collection = Collection([1, 2, 3, 4])
collection.put(1, 5)
collection.all()
# [1, 5, 3, 4]Note
It is equivalent to:
collection[1] = 5The reduce method reduces the collection to a single value,
passing the result of each iteration into the subsequent iteration:
collection = Collection([1, 2, 3])
collection.reduce(lambda result, item: (result or 0) + item)
# 6The value for result on the first iteration is None;
however, you can specify its initial value by passing a second argument to reduce:
collection.reduce(lambda result, item: result + item, 4)
# 10The reject method filters the collection using the given callback.
The callback should return True for any items it wishes to remove from the resulting collection:
collection = Collection([1, 2, 3, 4])
filtered = collection.reject(lambda item: item > 2)
filtered.all()
# [1, 2]For the inverse of reject, see the filter method.
The reverse method reverses the order of the collection's items:
collection = Collection([1, 2, 3, 4, 5])
reverse = collection.reverse()
reverse.all()
# [5, 4, 3, 2, 1]The serialize method converts the collection into a list.
If the collection's values are :ref:`ORM` models, the models will also be converted to dictionaries:
collection = Collection([User.find(1)])
collection.serialize()
# [{'id': 1, 'name': 'John'}]Warning
serialize also converts all of its nested objects.
If you want to get the underlying items as is, use the all method instead.
The shift method removes and returns the first item from the collection:
collection = Collection([1, 2, 3, 4, 5])
collection.shift()
# 1
collection.all()
# [2, 3, 4, 5]The sort method sorts the collection:
collection = Collection([5, 3, 1, 2, 4])
sorted = collection.sort()
sorted.all()
# [1, 2, 3, 4, 5]The sum method returns the sum of all items in the collection:
Collection([1, 2, 3, 4, 5]).sum()
# 15If the collection contains dictionaries or objects, you must pass a key to use for determining which values to sum:
collection = Collection([
{'name': 'JavaScript: The Good Parts', 'pages': 176},
{'name': 'JavaScript: The Defnitive Guide', 'pages': 1096}
])
collection.sum('pages')
# 1272In addition, you can pass your own callback to determine which values of the collection to sum:
collection = Collection([
{'name': 'Chair', 'colors': ['Black']},
{'name': 'Desk', 'colors': ['Black', 'Mahogany']},
{'name': 'Bookcase', 'colors': ['Red', 'Beige', 'Brown']}
])
collection.sum(lambda product: len(product['colors']))
# 6The take method returns a new collection with the specified number of items:
collection = Collection([0, 1, 2, 3, 4, 5])
chunk = collection.take(3)
chunk.all()
# [0, 1, 2]You can also pass a negative integer to take the specified amount of items from the end of the collection:
chunk = collection.chunk(-2)
chunk.all()
# [4, 5]The to_json method converts the collection into JSON:
collection = Collection([{'name': 'Desk', 'price': 200}])
collection.to_json()
# '[{"name": "Desk", "price": 200}]'The transform method iterates over the collection and calls the given callback
with each item in the collection.
The items in the collection will be replaced by the values returned by the callback:
collection = Collection([1, 2, 3, 4, 5])
collection.transform(lambda item: item * 2)
collection.all()
# [2, 4, 6, 8, 10]Warning
Unlike most other collection methods, transform modifies the collection itself.
If you wish to create a new collection instead, use the map method.
The unique method returns all of the unique items in the collection:
collection = Collection([1, 1, 2, 2, 3, 4, 2])
unique = collection.unique()
unique.all()
# [1, 2, 3, 4]When dealing with dictionaries or objects, you can specify the key used to determine uniqueness:
collection = Collection([
{'name': 'iPhone 6', 'brand': 'Apple', 'type': 'phone'},
{'name': 'iPhone 5', 'brand': 'Apple', 'type': 'phone'},
{'name': 'Apple Watch', 'brand': 'Apple', 'type': 'watch'},
{'name': 'Galaxy S6', 'brand': 'Samsung', 'type': 'phone'},
{'name': 'Galaxy Gear', 'brand': 'Samsung', 'type': 'watch'}
])
unique = collection.unique('brand')
unique.all()
# [
# {'name': 'iPhone 6', 'brand': 'Apple', 'type': 'phone'},
# {'name': 'Galaxy S6', 'brand': 'Samsung', 'type': 'phone'}
# ]You can also pass your own callback to determine item uniqueness:
unique = collection.unique(lambda item: item['brand'] + item['type'])
unique.all()
# [
# {'name': 'iPhone 6', 'brand': 'Apple', 'type': 'phone'},
# {'name': 'Apple Watch', 'brand': 'Apple', 'type': 'watch'},
# {'name': 'Galaxy S6', 'brand': 'Samsung', 'type': 'phone'},
# {'name': 'Galaxy Gear', 'brand': 'Samsung', 'type': 'watch'}
# ]The where method filters the collection by a given key / value pair:
collection = Collection([
{'name': 'Desk', 'price': 200},
{'name': 'Chair', 'price': 100},
{'name': 'Bookcase', 'price': 150},
{'name': 'Door', 'price': 100},
])
filtered = collection.where('price', 100)
filtered.all()
# [
# {'name': 'Chair', 'price': 100},
# {'name': 'Door', 'price': 100}
# ]The zip method merges together the values of the given list
with the values of the collection at the corresponding index:
collection = Collection(['Chair', 'Desk'])
zipped = collection.zip([100, 200])
zipped.all()
# [('Chair', 100), ('Desk', 200)]