Collections
Anytime your results return multiple values then an instance of Collection
is returned. This allows you to iterate over your values and has a lot of shorthand methods.
When using collections as a query result you can iterate over it as if the collection with a normal list:
Available Methods
Here is the updated list with min
added after merge
:
all
Returns the underlying list or dict represented by the collection:
avg
Returns the average of all items in the collection:
If the collection contains nested objects or dictionaries (e.g. for a collection of models), you must pass a key to use for determining which values to calculate the average:
chunk
Chunks a collection into multiple, smaller collections of a given size. Uses a generator to keep each chunk small. Useful for chunking large data sets where pulling too many results in memory will overload the application.
collapse
Collapses a collection of lists into a flat collection:
contains
Determines whether the collection contains a given item:
You can also pass a key / value pair to the contains method, which will determine if the given pair exists in the collection.
Finally, you may also pass a callback to the contains method to perform your own truth test:
count
Returns the total number of items in the collection. len()
standard python method can also be used.
diff
Returns the difference as a collection against another collection
each
Iterates over the items in the collection and passes each item to a given callback:
every
Creates a new collection by applying a given callback on every element:
filter
Filters the collection by a given callback, keeping only those items that pass a given truth test:
first
Returns the first item of the collection, if no arguments are given.
When given a truth test as callback, it returns the first element in the collection that passes the test:
flatten
Flattens a multi-dimensional collection into a single dimension:
forget
Removes an item from the collection by its key:
Unlike most other collection methods, forget
does not return a new modified collection; it modifies the collection it is called on.
for_page
Paginates the collection by returning a new collection containing the items that would be present on a given page number:
for_page(page, count)
takes the page number and the number of items to show per page.
get
Returns the item at a given key or index. If the key does not exist, None is returned. An optional default value can be passed as the second argument:
group_by
Returns a collection where items are grouped by the given key:
implode
Joins the items in a collection with ,
or the given glue string.
If the collection contains dictionaries or objects, you must pass the key of the attributes you wish to join:
is_empty
Returns True
if the collection is empty; otherwise, False
is returned:
last
Returns the last element in the collection if no arguments are given.
Returns the last element in the collection that passes the given truth test:
map
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:
If you want to transform the original collection, use the transform method.
map_into
Iterates through the collection and cast each value into the given class:
A class method can also be specified. Some additional keywords arguments can be passed to this method:
max
Retrieves max value of the collection:
If the collection contains dictionaries or objects, you must pass the key on which to compute max value:
merge
Merges the given list into the collection:
Unlike most other collection methods, merge
does not return a new modified collection; it modifies the collection it is called on.
min
Retrieves min value of the collection:
If the collection contains dictionaries or objects, you must pass the key on which to compute min value:
pluck
Retrieves all of the collection values for a given key:
A key can be given to pluck the collection into a dictionary with the given key
You can pass keep_nulls=False
to remove None
value in the collection.
pop
Removes and returns the last item from the collection:
prepend
Adds an item to the beginning of the collection:
pull
Removes and returns an item from the collection by its key:
push
Appends an item to the end of the collection:
put
Sets the given key and value in the collection:
random
Returns a random item from the collection
An integer count can be given to random
method to specify how many items you would like to randomly retrieve from the collection. A collection will always be returned when the items count is specified
If the collection length is smaller than specified count a ValueError
will be raised.
reduce
Reduces the collection to a single value, passing the result of each iteration into the subsequent iteration.
Initial value is 0
by default but can be overridden:
reject
It's the inverse of filter method. It filters the collection using the given callback. The callback should return True
for any items to remove from the resulting collection:
Unlike most other collection methods, reject
does not return a new modified collection; it modifies the collection it is called on.
reverse
Reverses the order of the items in the collection:
Unlike most other collection methods, reverse
does not return a new modified collection; it modifies the collection it is called on.
serialize
Converts the collection into a list. If the collection’s values are ORM models, the models will also be converted to dictionaries:
Be careful, serialize
also converts all of its nested objects. If you want to get the underlying items as is, use the all method instead.
shift
Removes and returns the first item from the collection:
sort
Sorts the collection:
sum
Returns the sum of all items in the collection:
If the collection contains dictionaries or objects, you must pass a key to use for determining which values to sum:
take
Returns a new collection with the specified number of items:
You can also pass a negative integer to take the specified amount of items from the end of the collection:
to_json
Converts the collection into JSON:
transform
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:
If you wish to create a new collection instead, use the map method.
unique
Returns all of the unique items in the collection:
When dealing with dictionaries or objects, you can specify the key used to determine uniqueness:
where
Filters the collection by a given key / value pair:
zip
Merges together the values of the given list with the values of the collection at the corresponding index:
Last updated