pydash-cheatsheet#
A simplified reference for pydash.
get#
Easy access to properties at any level
Get the value at any depth of a nested object based on the path described by `path`. If path
doesn't exist, `default` is returned.
Args:
obj: Object to process.
path: List or ``.`` delimited string of path describing path.
default: Default value to return if path doesn't exist. Defaults to ``None``.
Returns:
Value of `obj` at path.
Example:
>>> get({}, 'a.b.c') is None
True
>>> get({'a': {'b': {'c': [1, 2, 3, 4]}}}, 'a.b.c[1]')
2
>>> get({'a': {'b': {'c': [1, 2, 3, 4]}}}, 'a.b.c.1')
2
>>> get({'a': {'b': [0, {'c': [1, 2]}]}}, 'a.b.1.c.1')
2
>>> get({'a': {'b': [0, {'c': [1, 2]}]}}, ['a', 'b', 1, 'c', 1])
2
>>> get({'a': {'b': [0, {'c': [1, 2]}]}}, 'a.b.1.c.2') is None
True
.. versionadded:: 2.0.0
.. versionchanged:: 2.2.0
Support escaping "." delimiter in single string path key.
.. versionchanged:: 3.3.0
- Added :func:`get` as main definition and :func:`get_path` as alias.
- Made :func:`deep_get` an alias.
.. versionchanged:: 3.4.7
Fixed bug where an iterable default was iterated over instead of being returned when an
object path wasn't found.
.. versionchanged:: 4.0.0
- Support attribute access on `obj` if item access fails.
- Removed aliases ``get_path`` and ``deep_get``.
.. versionchanged:: 4.7.6
Fixed bug where getattr is used on Mappings and Sequence in Python 3.5+
set_#
Set property at any level
Variants: set_with
Sets the value of an object described by `path`. If any part of the object path doesn't exist,
it will be created.
Args:
obj: Object to modify.
path: Target path to set value to.
value: Value to set.
Returns:
Modified `obj`.
Warning:
`obj` is modified in place.
Example:
>>> set_({}, 'a.b.c', 1)
{'a': {'b': {'c': 1}}}
>>> set_({}, 'a.0.c', 1)
{'a': {'0': {'c': 1}}}
>>> set_([1, 2], '[2][0]', 1)
[1, 2, [1]]
>>> set_({}, 'a.b[0].c', 1)
{'a': {'b': [{'c': 1}]}}
.. versionadded:: 2.2.0
.. versionchanged:: 3.3.0
Added :func:`set_` as main definition and :func:`deep_set` as alias.
.. versionchanged:: 4.0.0
- Modify `obj` in place.
- Support creating default path values as ``list`` or ``dict`` based on whether key or index
substrings are used.
- Remove alias ``deep_set``.
has#
Check if property exists at any level
Checks if `path` exists as a key of `obj`.
Args:
obj: Object to test.
path: Path to test for. Can be a list of nested keys or a ``.`` delimited string of
path describing the path.
Returns:
Whether `obj` has `path`.
Example:
>>> has([1, 2, 3], 1)
True
>>> has({'a': 1, 'b': 2}, 'b')
True
>>> has({'a': 1, 'b': 2}, 'c')
False
>>> has({'a': {'b': [0, {'c': [1, 2]}]}}, 'a.b.1.c.1')
True
>>> has({'a': {'b': [0, {'c': [1, 2]}]}}, 'a.b.1.c.2')
False
.. versionadded:: 1.0.0
.. versionchanged:: 3.0.0
Return ``False`` on ``ValueError`` when checking path.
.. versionchanged:: 3.3.0
- Added :func:`deep_has` as alias.
- Added :func:`has_path` as alias.
.. versionchanged:: 4.0.0
Removed aliases ``deep_has`` and ``has_path``.
find#
Find first element that passes a criterion function
Variants: find_index, find_last, find_last_index
Iterates over elements of a collection, returning the first element that the predicate returns
truthy for.
Args:
collection: Collection to iterate over.
predicate: Predicate applied per iteration.
Returns:
First element found or ``None``.
Example:
>>> find([1, 2, 3, 4], lambda x: x >= 3)
3
>>> find([{'a': 1}, {'b': 2}, {'a': 1, 'b': 2}], {'a': 1})
{'a': 1}
.. versionadded:: 1.0.0
.. versionchanged:: 4.0.0
Removed aliases ``detect`` and ``find_where``.
index_of#
Find the index of an element
Variants: last_index_of
Gets the index at which the first occurrence of value is found.
Args:
array: List to search.
value: Value to search for.
from_index: Index to search from.
Returns:
Index of found item or ``-1`` if not found.
Example:
>>> index_of([1, 2, 3, 4], 2)
1
>>> index_of([2, 1, 2, 3], 2, from_index=1)
2
.. versionadded:: 1.0.0
pluck#
Retrieve property values from collections
Retrieves the value of a specified property from all elements in the collection.
Args:
collection: List of dicts.
path: Collection's path to pluck
Returns:
Plucked list.
Example:
>>> pluck([{'a': 1, 'b': 2}, {'a': 3, 'b': 4}, {'a': 5, 'b': 6}], 'a')
[1, 3, 5]
>>> pluck([[[0, 1]], [[2, 3]], [[4, 5]]], '0.1')
[1, 3, 5]
>>> pluck([{'a': {'b': 1}}, {'a': {'b': 2}}], 'a.b')
[1, 2]
>>> pluck([{'a': {'b': [0, 1]}}, {'a': {'b': [2, 3]}}], 'a.b.1')
[1, 3]
>>> pluck([{'a': {'b': [0, 1]}}, {'a': {'b': [2, 3]}}], ['a', 'b', 1])
[1, 3]
.. versionadded:: 1.0.0
.. versionchanged:: 4.0.0
Function removed.
.. versionchanged:: 4.0.1
Made property access deep.
pull#
Removes specific elements from array
Variants: pull_all_by, pull_all_with, pull_at, remove, take, without
Removes all provided values from the given array.
Args:
array: List to pull from.
values: Values to remove.
Returns:
Modified `array`.
Warning:
`array` is modified in place.
Example:
>>> pull([1, 2, 2, 3, 3, 4], 2, 3)
[1, 4]
.. versionadded:: 1.0.0
.. versionchanged:: 4.0.0
:func:`pull` method now calls :func:`pull_all` method for the desired
functionality.
rename_keys#
New dict with renamed keys
Rename the keys of `obj` using `key_map` and return new object.
Args:
obj: Object to rename.
key_map: Renaming map whose keys correspond to existing keys in `obj` and whose
values are the new key name.
Returns:
Renamed `obj`.
Example:
>>> obj = rename_keys({'a': 1, 'b': 2, 'c': 3}, {'a': 'A', 'b': 'B'})
>>> obj == {'A': 1, 'B': 2, 'c': 3}
True
.. versionadded:: 2.0.0
flatten#
Flatten an array by one level
Variants: flatten_deep, flatten_depth
Flattens array a single level deep.
Args:
array: List to flatten.
Returns:
Flattened list.
Example:
>>> flatten([[1], [2, [3]], [[4]]])
[1, 2, [3], [4]]
.. versionadded:: 1.0.0
.. versionchanged:: 2.0.0
Removed `callback` option. Added ``is_deep`` option. Made it shallow
by default.
.. versionchanged:: 4.0.0
Removed ``is_deep`` option. Use :func:`flatten_deep` instead.
count_by#
Count occurences of values in sequence
Creates an object composed of keys generated from the results of running each element of
`collection` through the iteratee.
Args:
collection: Collection to iterate over.
iteratee: Iteratee applied per iteration.
Returns:
Dict containing counts by key.
Example:
>>> results = count_by([1, 2, 1, 2, 3, 4])
>>> assert results == {1: 2, 2: 2, 3: 1, 4: 1}
>>> results = count_by(['a', 'A', 'B', 'b'], lambda x: x.lower())
>>> assert results == {'a': 2, 'b': 2}
>>> results = count_by({'a': 1, 'b': 1, 'c': 3, 'd': 3})
>>> assert results == {1: 2, 3: 2}
.. versionadded:: 1.0.0
camel_case#
Text to camel case
Variants: kebab_case, human_case, snake_case, slugify etc
sample_size#
Retrieve random sample of elements
Retrieves list of `n` random elements from a collection.
Args:
collection: Collection to iterate over.
n: Number of random samples to return.
Returns:
List of `n` sampled collection values.
Examples:
>>> items = [1, 2, 3, 4, 5]
>>> results = sample_size(items, 2)
>>> assert len(results) == 2
>>> assert set(items).intersection(results) == set(results)
.. versionadded:: 4.0.0
chunk#
Separate elements into chunks
Creates a list of elements split into groups the length of `size`. If `array` can't be split
evenly, the final chunk will be the remaining elements.
Args:
array: List to chunk.
size: Chunk size. Defaults to ``1``.
Returns:
New list containing chunks of `array`.
Example:
>>> chunk([1, 2, 3, 4, 5], 2)
[[1, 2], [3, 4], [5]]
.. versionadded:: 1.1.0
compact#
Take falsey values out
difference#
Return elements that are in the first array but not in the others
Variants: difference_by, difference_with
take_while#
Conditional slice
Variants: drop_right_while, drop_while, drop_right_while
Creates a slice of `array` with elements taken from the beginning. Elements are taken until the
`predicate` returns falsey. The `predicate` is invoked with three arguments: ``(value, index,
array)``.
Args:
array: List to process.
predicate: Predicate called per iteration
Returns:
Taken list.
Example:
>>> take_while([1, 2, 3, 4], lambda x: x < 3)
[1, 2]
.. versionadded:: 1.1.0
duplicates#
Returns duplicate values
Creates a unique list of duplicate values from `array`. If iteratee is passed, each element of
array is passed through an iteratee before duplicates are computed. The iteratee is invoked with
three arguments: ``(value, index, array)``. If an object path is passed for iteratee, the
created iteratee will return the path value of the given element. If an object is passed for
iteratee, the created filter style iteratee will return ``True`` for elements that have the
properties of the given object, else ``False``.
Args:
array: List to process.
iteratee: Iteratee applied per iteration.
Returns:
List of duplicates.
Example:
>>> duplicates([0, 1, 3, 2, 3, 1])
[3, 1]
.. versionadded:: 3.0.0
attempt#
Return function result or caught exception
memoize#
Cache function results
Creates a function that memoizes the result of `func`. If `resolver` is provided it will be used
to determine the cache key for storing the result based on the arguments provided to the
memoized function. By default, all arguments provided to the memoized function are used as the
cache key. The result cache is exposed as the cache property on the memoized function.
Args:
func: Function to memoize.
resolver: Function that returns the cache key to use.
Returns:
Memoized function.
Example:
>>> ident = memoize(identity)
>>> ident(1)
1
>>> ident.cache['(1,){}'] == 1
True
>>> ident(1, 2, 3)
1
>>> ident.cache['(1, 2, 3){}'] == 1
True
.. versionadded:: 1.0.0
debounce#
Delay execution of a function
Variants: delay
Creates a function that will delay the execution of `func` until after `wait` milliseconds have
elapsed since the last time it was invoked. Subsequent calls to the debounced function will
return the result of the last `func` call.
Args:
func: Function to execute.
wait: Milliseconds to wait before executing `func`.
max_wait (optional): Maximum time to wait before executing `func`.
Returns:
Function wrapped in a :class:`Debounce` context.
.. versionadded:: 1.0.0
curry#
Partial application of functions
Creates a function that accepts one or more arguments of `func` that when invoked either
executes `func` returning its result (if all `func` arguments have been provided) or returns a
function that accepts one or more of the remaining `func` arguments, and so on.
Args:
func: Function to curry.
arity: Number of function arguments that can be accepted by curried
function. Default is to use the number of arguments that are accepted by `func`.
Returns:
Function wrapped in a :class:`Curry` context.
Example:
>>> func = lambda a, b, c: (a, b, c)
>>> currier = curry(func)
>>> currier = currier(1)
>>> assert isinstance(currier, Curry)
>>> currier = currier(2)
>>> assert isinstance(currier, Curry)
>>> currier = currier(3)
>>> currier
(1, 2, 3)
.. versionadded:: 1.0.0
xor#
Exclusive or of sequences
Variants: xor_by, xor_with