FP Variations

Pydash exposes a variant set of its functions which are auto-curried, iteratee-first, data-last, and non-mutating. These functions are available in pydash.fp. The creation of this module is inspired by, and reflects much of the functionality in lodash/fp, however it differs in a few significant ways as well.

Currying

The fp variant functions are curried, meaning that one can provide arguments in multiple function calls, like so:

# the following two calls are equivalent
fp.union_by(lambda x: x % 2, [1, 2, 3], [2, 3, 4])

fp.union_by(lambda x: x % 2)([1, 2, 3])([2, 3, 4])

This means that each function must receive a fixed minimum number of arguments. Keyword arguments are not supported. As much as possible, support is provided for multiple data arguments where the original function allows it:

fp.intersection_by(round)([1.2, 1.5, 1.7, 2.8], [0.9, 3.2], [1.1])

In some cases, a default parameter value can be specified by passing None:

fp.intersection_by(None, [1, 2, 3], [2, 3, 4])

Rearranged Arguments

Function arguments are rearranged to facilitate composition. In most cases, iteratee is given first and data last.

Placeholder

It is possible to provide arguments in a different order by use of the placeholder. A placeholder is filled by the first available argument:

from pydash.fp import _

# the following two calls are equivalent
fp.intersperse(0)([1, 2, 3])

fp.intersperse(_, [1, 2, 3])(0)

No Mutation

Some pydash functions mutate the target object. The fp variants of these functions are non-mutating.

Iteratee Capping

In contradistinction to lodash/fp, iteratees in pydash.fp are not capped.

Documentation

The documentation for fp variant functions is automatically generated from the pydash docstrings. All effort is made to ensure that the conversion is complete and correct. Please report any errors.

astor is used in converting the code examples. This package is not a hard requirement for using the library, but if it is not available, the examples will be left out of the documentation.

Arrays

pydash.fp.arrays.chunk(*args)

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.

Arity: 2

Parameters:
  • size (int) – Chunk size. Defaults to 1.
  • array (list) – List to chunk.
Returns:

New list containing chunks of array.

Return type:

list

Example

>>> chunk(2, [1, 2, 3, 4, 5])
[[1, 2], [3, 4], [5]]

New in version 1.1.0.

pydash.fp.arrays.compact(array)[source]

Creates a list with all falsey values of array removed.

Parameters:array (list) – List to compact.
Returns:Compacted list.
Return type:list

Example

>>> compact(['', 1, 0, True, False, None])
[1, True]

New in version 1.0.0.

pydash.fp.arrays.concat(*args)

Concatenates zero or more lists into one.

Arity: 2

Parameters:arrays (list) – Lists to concatenate.
Returns:Concatenated list.
Return type:list

Example

>>> concat([1, 2], [3, 4], [[5], [6]])
[1, 2, 3, 4, [5], [6]]

New in version 2.0.0.

Changed in version 4.0.0: Renamed from cat to concat.

pydash.fp.arrays.difference(*args)

Creates a list of list elements not present in others.

Arity: 2

Parameters:
  • array (list) – List to process.
  • others (list) – Lists to check.
Returns:

Difference between others.

Return type:

list

Example

>>> difference([1, 2, 3], [1])
[3]

New in version 1.0.0.

pydash.fp.arrays.difference_by(*args)

This method is like difference() except that it accepts an iteratee which is invoked for each element of each array to generate the criterion by which they’re compared. The order and references of result values are determined by array. The iteratee is invoked with one argument: (value).

Arity: 3

Parameters:
  • iteratee (mixed) – Function to transform the elements of the arrays. Defaults to identity().
  • array (list) – The array to find the difference of.
  • others (list) – Lists to check for difference with array.
Returns:

Difference between others.

Return type:

list

Example

>>> difference_by(round, [1.2, 1.5, 1.7, 2.8], [0.9, 3.2])
[1.5, 1.7]

New in version 4.0.0.

pydash.fp.arrays.difference_with(*args)

This method is like difference() except that it accepts a comparator which is invoked to compare the elements of all arrays. The order and references of result values are determined by the first array. The comparator is invoked with two arguments: (arr_val, oth_val).

Arity: 3

Parameters:
  • comparator (callable) – Function to compare the elements of the arrays. Defaults to is_equal().
  • array (list) – The array to find the difference of.
  • others (list) – Lists to check for difference with array.
Returns:

Difference between others.

Return type:

list

Example

>>> array = ['apple', 'banana', 'pear']
>>> others = ['avocado', 'pumpkin'], ['peach']
>>> comparator = lambda a, b: a[0] == b[0]
>>> difference_with(comparator, array, *others)
['banana']

New in version 4.0.0.

pydash.fp.arrays.drop(*args)

Creates a slice of array with n elements dropped from the beginning.

Arity: 2

Parameters:
  • n (int) – Number of elements to drop. Defaults to 1.
  • array (list) – List to process.
Returns:

Dropped list.

Return type:

list

Example

>>> drop(2, [1, 2, 3, 4])
[3, 4]

New in version 1.0.0.

Changed in version 1.1.0: Added n argument and removed as alias of rest().

Changed in version 3.0.0: Made n default to 1.

pydash.fp.arrays.drop_while(*args)

Creates a slice of array excluding elements dropped from the beginning. Elements are dropped until the predicate returns falsey. The predicate is invoked with three arguments: (value, index, array).

Arity: 2

Parameters:
  • predicate (mixed) – Predicate called per iteration
  • array (list) – List to process.
Returns:

Dropped list.

Return type:

list

Example

>>> drop_while(lambda x: x < 3, [1, 2, 3, 4])
[3, 4]

New in version 1.1.0.

pydash.fp.arrays.drop_right(*args)

Creates a slice of array with n elements dropped from the end.

Arity: 2

Parameters:
  • n (int) – Number of elements to drop. Defaults to 1.
  • array (list) – List to process.
Returns:

Dropped list.

Return type:

list

Example

>>> drop_right(2, [1, 2, 3, 4])
[1, 2]

New in version 1.1.0.

Changed in version 3.0.0: Made n default to 1.

pydash.fp.arrays.drop_right_while(*args)

Creates a slice of array excluding elements dropped from the end. Elements are dropped until the predicate returns falsey. The predicate is invoked with three arguments: (value, index, array).

Arity: 2

Parameters:
  • predicate (mixed) – Predicate called per iteration
  • array (list) – List to process.
Returns:

Dropped list.

Return type:

list

Example

>>> drop_right_while(lambda x: x >= 3, [1, 2, 3, 4])
[1, 2]

New in version 1.1.0.

pydash.fp.arrays.duplicates(*args)

Creates a unique list of duplicate values from array. If iteratee is passed, each element of array is passed through a 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.

Arity: 2

Parameters:
  • iteratee (mixed) – Iteratee applied per iteration.
  • array (list) – List to process.
Returns:

List of duplicates.

Return type:

list

Example

>>> duplicates([0, 1, 3, 2, 3, 1])
[3, 1]

New in version 3.0.0.

pydash.fp.arrays.fill(*args)

Fills elements of array with value from start up to, but not including, end.

Arity: 4

Parameters:
  • start (int) – Index to start filling. Defaults to 0.
  • end (int) – Index to end filling. Defaults to len(array).
  • value (mixed) – Value to fill with.
  • array (list) – List to fill.
Returns:

Filled array.

Return type:

list

Example

>>> fill(1, 3, 0, [1, 2, 3, 4, 5])
[1, 0, 0, 4, 5]
>>> fill(0, 100, 0, [1, 2, 3, 4, 5])
[0, 0, 0, 0, 0]

New in version 3.1.0.

pydash.fp.arrays.find_index(*args)

This method is similar to pydash.collections.find(), except that it returns the index of the element that passes the predicate check, instead of the element itself.

Arity: 2

Parameters:
  • predicate (mixed) – Predicate applied per iteration.
  • array (list) – List to process.
Returns:

Index of found item or -1 if not found.

Return type:

int

Example

>>> find_index(lambda x: x >= 3, [1, 2, 3, 4])
2
>>> find_index(lambda x: x > 4, [1, 2, 3, 4])
-1

New in version 1.0.0.

pydash.fp.arrays.find_last_index(*args)

This method is similar to find_index(), except that it iterates over elements from right to left.

Arity: 2

Parameters:
  • predicate (mixed) – Predicate applied per iteration.
  • array (list) – List to process.
Returns:

Index of found item or -1 if not found.

Return type:

int

Example

>>> find_last_index(lambda x: x >= 3, [1, 2, 3, 4])
3
>>> find_index([1, 2, 3, 4], lambda x: x > 4)
-1

New in version 1.0.0.

pydash.fp.arrays.flatten(array)[source]

Flattens a nested array. If is_deep is True the array is recursively flattened, otherwise it is only flattened a single level.

Parameters:array (list) – List to flatten.
Returns:Flattened list.
Return type:list

Example

>>> flatten([[1], [2, [3]], [[4]]])
[1, 2, [3], [4]]

New in version 1.0.0.

Changed in version 2.0.0: Removed callback option. Added is_deep option. Made it shallow by default.

Changed in version 4.0.0: Removed is_deep option. Use flatten_deep() instead.

pydash.fp.arrays.flatten_deep(array)[source]

Flattens a nested array recursively. This is the same as calling flatten(array, is_deep=True).

Parameters:array (list) – List to flatten.
Returns:Flattened list.
Return type:list

Example

>>> flatten_deep([[1], [2, [3]], [[4]]])
[1, 2, 3, 4]

New in version 2.0.0.

pydash.fp.arrays.flatten_depth(*args)

Recursively flatten array up to depth times.

Arity: 2

Parameters:
  • depth (int) – Depth to flatten to. Defaults to 1.
  • array (list) – List to flatten.
Returns:

Flattened list.

Return type:

list

Example

>>> flatten_depth(1, [[[1], [2, [3]], [[4]]]])
[[1], [2, [3]], [[4]]]
>>> flatten_depth(2, [[[1], [2, [3]], [[4]]]])
[1, 2, [3], [4]]
>>> flatten_depth(3, [[[1], [2, [3]], [[4]]]])
[1, 2, 3, 4]
>>> flatten_depth(4, [[[1], [2, [3]], [[4]]]])
[1, 2, 3, 4]

New in version 4.0.0.

pydash.fp.arrays.from_pairs(pairs)[source]

Returns a dict from the given list of pairs.

Parameters:pairs (list) – List of key-value pairs.
Returns:dict

Example

>>> from_pairs([['a', 1], ['b', 2]]) == {'a': 1, 'b': 2}
True

New in version 4.0.0.

pydash.fp.arrays.head(array)[source]

Return the first element of array.

Parameters:array (list) – List to process.
Returns:First element of list.
Return type:mixed

Example

>>> head([1, 2, 3, 4])
1

New in version 1.0.0.

Changed in version Renamed: from first to head.

pydash.fp.arrays.index_of(*args)

Gets the index at which the first occurrence of value is found.

Arity: 2

Parameters:
  • value (mixed) – Value to search for.
  • array (list) – List to search.
  • from_index (int, optional) – Index to search from.
Returns:

Index of found item or -1 if not found.

Return type:

int

Example

>>> index_of(2, [1, 2, 3, 4])
1
>>> index_of(2, [2, 1, 2, 3])
2

New in version 1.0.0.

pydash.fp.arrays.initial(array)[source]

Return all but the last element of array.

Parameters:array (list) – List to process.
Returns:Initial part of array.
Return type:list

Example

>>> initial([1, 2, 3, 4])
[1, 2, 3]

New in version 1.0.0.

pydash.fp.arrays.intercalate(*args)

Like intersperse() for lists of lists but shallowly flattening the result.

Arity: 2

Parameters:
  • separator (mixed) – Element to insert.
  • array (list) – List to intercalate.
Returns:

Intercalated list.

Return type:

list

Example

>>> intercalate('x', [1, [2], [3], 4])
[1, 'x', 2, 'x', 3, 'x', 4]

New in version 2.0.0.

pydash.fp.arrays.interleave(*args)

Merge multiple lists into a single list by inserting the next element of each list by sequential round-robin into the new list.

Arity: 2

Parameters:arrays (list) – Lists to interleave.
Returns:Interleaved list.
Return type:list

Example

>>> interleave([1, 2, 3], [4, 5, 6], [7, 8, 9])
[1, 4, 7, 2, 5, 8, 3, 6, 9]

New in version 2.0.0.

pydash.fp.arrays.intersection(*args)

Computes the intersection of all the passed-in arrays.

Arity: 2

Parameters:
  • array (list) – The array to find the intersection of.
  • others (list) – Lists to check for intersection with array.
Returns:

Intersection of provided lists.

Return type:

list

Example

>>> intersection([1, 2, 3], [1, 2, 3, 4, 5])
[2, 3]
>>> intersection([1, 2, 3])
[1, 2, 3]

New in version 1.0.0.

Changed in version 4.0.0: Support finding intersection of unhashable types.

pydash.fp.arrays.intersection_by(*args)

This method is like intersection() except that it accepts an iteratee which is invoked for each element of each array to generate the criterion by which they’re compared. The order and references of result values are determined by array. The iteratee is invoked with one argument: (value).

Arity: 3

Parameters:
  • iteratee (mixed) – Function to transform the elements of the arrays. Defaults to identity().
  • array (list) – The array to find the intersection of.
  • others (list) – Lists to check for intersection with array.
Returns:

Intersection of provided lists.

Return type:

list

Example

>>> intersection_by(round, [1.2, 1.5, 1.7, 2.8], [0.9, 3.2])
[1.2, 2.8]

New in version 4.0.0.

pydash.fp.arrays.intersection_with(*args)

This method is like intersection() except that it accepts a comparator which is invoked to compare the elements of all arrays. The order and references of result values are determined by the first array. The comparator is invoked with two arguments: (arr_val, oth_val).

Arity: 3

Parameters:
  • comparator (callable) – Function to compare the elements of the arrays. Defaults to is_equal().
  • array (list) – The array to find the intersection of.
  • others (list) – Lists to check for intersection with array.
Returns:

Intersection of provided lists.

Return type:

list

Example

>>> array = ['apple', 'banana', 'pear']
>>> others = ['avocado', 'pumpkin'], ['peach']
>>> comparator = lambda a, b: a[0] == b[0]
>>> intersection_with(comparator, array, *others)
['pear']

New in version 4.0.0.

pydash.fp.arrays.intersperse(*args)

Insert a separating element between the elements of array.

Arity: 2

Parameters:
  • separator (mixed) – Element to insert.
  • array (list) – List to intersperse.
Returns:

Interspersed list.

Return type:

list

Example

>>> intersperse('x', [1, [2], [3], 4])
[1, 'x', [2], 'x', [3], 'x', 4]

New in version 2.0.0.

pydash.fp.arrays.last(array)[source]

Return the last element of array.

Parameters:array (list) – List to process.
Returns:Last part of array.
Return type:mixed

Example

>>> last([1, 2, 3, 4])
4

New in version 1.0.0.

pydash.fp.arrays.last_index_of(*args)

Gets the index at which the last occurrence of value is found.

Arity: 2

Parameters:
  • value (mixed) – Value to search for.
  • array (list) – List to search.
  • from_index (int, optional) – Index to search from.
Returns:

Index of found item or False if not found.

Return type:

int

Example

>>> last_index_of(2, [1, 2, 2, 4])
2
>>> last_index_of(2, [1, 2, 2, 4])
1

New in version 1.0.0.

pydash.fp.arrays.mapcat(*args)

Map a iteratee to each element of a list and concatenate the results into a single list using cat().

Arity: 2

Parameters:
  • iteratee (mixed) – Iteratee to apply to each element.
  • array (list) – List to map and concatenate.
Returns:

Mapped and concatenated list.

Return type:

list

Example

>>> mapcat(lambda x: list(range(x)), range(4))
[0, 0, 1, 0, 1, 2]

New in version 2.0.0.

pydash.fp.arrays.nth(*args)

Gets the element at index n of array.

Arity: 2

Parameters:
  • pos (int) – Index of element to return.
  • array (list) – List passed in by the user.
Returns:

Returns the element at pos.

Return type:

mixed

Example

>>> nth(0, [1, 2, 3])
1
>>> nth(2, [3, 4, 5, 6])
5
>>> nth(-1, [11, 22, 33])
33
>>> nth([11, 22, 33])
11

New in version 4.0.0.

pydash.fp.arrays.pull(*args)

Removes all provided values from the given array.

Arity: 2

Parameters:
  • values (mixed) – Values to remove.
  • array (list) – List to pull from.
Returns:

Resulting array.

Return type:

list

Note

The fp variant of pull takes exactly 2 arguments

Example

>>> pull(2, [1, 2, 2, 3, 3, 4])
[1, 4]

New in version 1.0.0.

Changed in version 4.0.0: pull() method now calls pull_all() method for the desired functionality.

pydash.fp.arrays.pull_all(*args)

Removes all provided values from the given array.

Arity: 2

Parameters:
  • values (list) – Values to remove.
  • array (list) – Array to modify.
Returns:

Resulting array.

Return type:

list

Example

>>> pull_all([2, 3], [1, 2, 2, 3, 3, 4])
[1, 4]

New in version 4.0.0.

pydash.fp.arrays.pull_all_by(*args)

This method is like pull_all() except that it accepts iteratee which is invoked for each element of array and values to generate the criterion by which they’re compared. The iteratee is invoked with one argument: (value).

Arity: 3

Parameters:
  • iteratee (mixed) – Function to transform the elements of the arrays. Defaults to identity().
  • values (list) – Values to remove.
  • array (list) – Array to modify.
Returns:

Resulting array.

Return type:

list

Example

>>> array = [{'x': 1}, {'x': 2}, {'x': 3}, {'x': 1}]
>>> pull_all_by('x', [{'x': 1}, {'x': 3}], array)
[{'x': 2}]

New in version 4.0.0.

pydash.fp.arrays.pull_all_with(*args)

This method is like pull_all() except that it accepts comparator which is invoked to compare elements of array to values. The comparator is invoked with two arguments: (arr_val, oth_val).

Arity: 3

Parameters:
  • comparator (callable) – Function to compare the elements of the arrays. Defaults to is_equal().
  • values (list) – Values to remove.
  • array (list) – Array to modify.
Returns:

Resulting array.

Return type:

list

Example

>>> array = [{'x': 1, 'y': 2}, {'x': 3, 'y': 4}, {'x': 5, 'y': 6}]
>>> res = pull_all_with(lambda a, b: a == b, [{'x': 3, 'y': 4}], array)
>>> res == [{'x': 1, 'y': 2}, {'x': 5, 'y': 6}]
True
>>> array = [{'x': 1, 'y': 2}, {'x': 3, 'y': 4}, {'x': 5, 'y': 6}]
>>> res = pull_all_with(lambda a, b: a != b, [{'x': 3, 'y': 4}], array)
>>> res == [{'x': 3, 'y': 4}]
True

New in version 4.0.0.

pydash.fp.arrays.pull_at(*args)

Removes elements from array corresponding to the specified indexes and returns a list of the removed elements. Indexes may be specified as a list of indexes or as individual arguments.

Arity: 2

Parameters:
  • indexes (int) – Indexes to pull.
  • array (list) – List to pull from.
Returns:

Resulting array.

Return type:

list

Note

The fp variant of pull_at takes exactly 2 arguments

Example

>>> pull_at(0, [1, 2, 3, 4])
[2, 4]

New in version 1.1.0.

pydash.fp.arrays.remove(*args)

Removes all elements from a list that the predicate returns truthy for and returns an array of removed elements.

Arity: 2

Parameters:
  • predicate (mixed) – Predicate applied per iteration.
  • array (list) – List to remove elements from.
Returns:

Removed elements of array.

Return type:

list

Example

>>> array = [1, 2, 3, 4]
>>> items = remove(lambda x: x >= 3, array)
>>> items
[3, 4]
>>> array
[1, 2]

New in version 1.0.0.

pydash.fp.arrays.repack(*args)

Repacks an iterable for destructuring assignment to provide extended iterable unpacking functionality in python 2.7

Arity: 2

Parameters:
  • lhs (str) – desired assignment structure, would be valid lhs in python 3
  • *items – items to repack for assignment
Returns:

items structured for assigment

Return type:

tuple

Example

>>> repack('a, *b, c', 1, 2, 3, 4)
(1, (2, 3), 4)
pydash.fp.arrays.reverse(array)[source]

Return array in reverse order.

Parameters:array (list|string) – Object to process.
Returns:Reverse of object.
Return type:list|string

Example

>>> reverse([1, 2, 3, 4])
[4, 3, 2, 1]

New in version 2.2.0.

pydash.fp.arrays.slice_(*args)

Slices array from the start index up to, but not including, the end index.

Arity: 3

Parameters:
  • start (int) – Start index. Defaults to 0.
  • end (int) – End index. Defaults to selecting the value at start index.
  • array (list) – Array to slice.
Returns:

Sliced list.

Return type:

list

Example

>>> slice_([1, 2, 3, 4])
[1]
>>> slice_(1, 3, [1, 2, 3, 4])
[2, 3]

New in version 1.1.0.

pydash.fp.arrays.sorted_index(*args)

Uses a binary search to determine the lowest index at which value should be inserted into array in order to maintain its sort order.

Arity: 2

Parameters:
  • value (mixed) – Value to evaluate.
  • array (list) – List to inspect.
Returns:

Returns the index at which value should be inserted into array.

Return type:

int

Example

>>> sorted_index(2, [1, 2, 2, 3, 4])
1

New in version 1.0.0.

Changed in version 4.0.0: Move iteratee support to sorted_index_by().

pydash.fp.arrays.sorted_index_by(*args)

This method is like sorted_index() except that it accepts iteratee which is invoked for value and each element of array to compute their sort ranking. The iteratee is invoked with one argument: (value).

Arity: 3

Parameters:
  • value (mixed) – Value to evaluate.
  • iteratee (mixed) – The iteratee invoked per element. Defaults to identity().
  • array (list) – List to inspect.
Returns:

Returns the index at which value should be inserted into array.

Return type:

int

Example

>>> array = [{'x': 4}, {'x': 5}]
>>> sorted_index_by({'x': 4}, lambda o: o['x'], array)
0
>>> sorted_index_by({'x': 4}, 'x', array)
0

New in version 4.0.0.

pydash.fp.arrays.sorted_index_of(*args)

Returns the index of the matched value from the sorted array, else -1.

Arity: 2

Parameters:
  • value (mixed) – Value to search for.
  • array (list) – Array to inspect.
Returns:

Returns the index of the first matched value, else -1.

Return type:

int

Example

>>> sorted_index_of(3, [3, 5, 7, 10])
0
>>> sorted_index_of(10, [10, 10, 5, 7, 3])
-1

New in version 4.0.0.

pydash.fp.arrays.sorted_last_index(*args)

This method is like sorted_index() except that it returns the highest index at which value should be inserted into array in order to maintain its sort order.

Arity: 2

Parameters:
  • value (mixed) – Value to evaluate.
  • array (list) – List to inspect.
Returns:

Returns the index at which value should be inserted into array.

Return type:

int

Example

>>> sorted_last_index(2, [1, 2, 2, 3, 4])
3

New in version 1.1.0.

Changed in version 4.0.0: Move iteratee support to sorted_last_index_by().

pydash.fp.arrays.sorted_last_index_by(*args)

This method is like sorted_last_index() except that it accepts iteratee which is invoked for value and each element of array to compute their sort ranking. The iteratee is invoked with one argument: (value).

Arity: 3

Parameters:
  • value (mixed) – Value to evaluate.
  • iteratee (mixed) – The iteratee invoked per element. Defaults to identity().
  • array (list) – List to inspect.
Returns:

Returns the index at which value should be inserted into array.

Return type:

int

Example

>>> array = [{'x': 4}, {'x': 5}]
>>> sorted_last_index_by({'x': 4}, lambda o: o['x'], array)
1
>>> sorted_last_index_by({'x': 4}, 'x', array)
1
pydash.fp.arrays.sorted_last_index_of(*args)

This method is like last_index_of() except that it performs a binary search on a sorted array.

Arity: 2

Parameters:
  • value (mixed) – Value to search for.
  • array (list) – Array to inspect.
Returns:

Returns the index of the matched value, else -1.

Return type:

int

Example

>>> sorted_last_index_of(5, [4, 5, 5, 5, 6])
3
>>> sorted_last_index_of(6, [6, 5, 5, 5, 4])
-1

New in version 4.0.0.

pydash.fp.arrays.sorted_uniq(array)[source]

Return sorted array with unique elements.

Parameters:array (list) – List of values to be sorted.
Returns:List of unique elements in a sorted fashion.
Return type:list

Example

>>> sorted_uniq([4, 2, 2, 5])
[2, 4, 5]
>>> sorted_uniq([-2, -2, 4, 1])
[-2, 1, 4]

New in version 4.0.0.

pydash.fp.arrays.sorted_uniq_by(*args)

This method is like sorted_uniq() except that it accepts iteratee which is invoked for each element in array to generate the criterion by which uniqueness is computed. The order of result values is determined by the order they occur in the array. The iteratee is invoked with one argument: (value).

Arity: 2

Parameters:
  • iteratee (mixed) – Function to transform the elements of the arrays. Defaults to identity().
  • array (list) – List of values to be sorted.
Returns:

Unique list.

Return type:

list

Example

>>> sorted_uniq_by(lambda val: val % 2, [3, 2, 1, 3, 2, 1])
[2, 3]

New in version 4.0.0.

pydash.fp.arrays.split_at(*args)

Returns a list of two lists composed of the split of array at index.

Arity: 2

Parameters:
  • index (int) – Index to split at.
  • array (list) – List to split.
Returns:

Split list.

Return type:

list

Example

>>> split_at(2, [1, 2, 3, 4])
[[1, 2], [3, 4]]

New in version 2.0.0.

pydash.fp.arrays.tail(array)[source]

Return all but the first element of array.

Parameters:array (list) – List to process.
Returns:Rest of the list.
Return type:list

Example

>>> tail([1, 2, 3, 4])
[2, 3, 4]

New in version 1.0.0.

Changed in version 4.0.0: Renamed from rest to tail.

pydash.fp.arrays.take(*args)

Creates a slice of array with n elements taken from the beginning.

Arity: 2

Parameters:
  • n (int) – Number of elements to take. Defaults to 1.
  • array (list) – List to process.
Returns:

Taken list.

Return type:

list

Example

>>> take(2, [1, 2, 3, 4])
[1, 2]

New in version 1.0.0.

Changed in version 1.1.0: Added n argument and removed as alias of first().

Changed in version 3.0.0: Made n default to 1.

pydash.fp.arrays.take_right(*args)

Creates a slice of array with n elements taken from the end.

Arity: 2

Parameters:
  • n (int) – Number of elements to take. Defaults to 1.
  • array (list) – List to process.
Returns:

Taken list.

Return type:

list

Example

>>> take_right(2, [1, 2, 3, 4])
[3, 4]

New in version 1.1.0.

Changed in version 3.0.0: Made n default to 1.

pydash.fp.arrays.take_right_while(*args)

Creates a slice of array with elements taken from the end. Elements are taken until the predicate returns falsey. The predicate is invoked with three arguments: (value, index, array).

Arity: 2

Parameters:
  • predicate (mixed) – Predicate called per iteration
  • array (list) – List to process.
Returns:

Dropped list.

Return type:

list

Example

>>> take_right_while(lambda x: x >= 3, [1, 2, 3, 4])
[3, 4]

New in version 1.1.0.

pydash.fp.arrays.take_while(*args)

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).

Arity: 2

Parameters:
  • predicate (mixed) – Predicate called per iteration
  • array (list) – List to process.
Returns:

Taken list.

Return type:

list

Example

>>> take_while(lambda x: x < 3, [1, 2, 3, 4])
[1, 2]

New in version 1.1.0.

pydash.fp.arrays.union(*args)

Computes the union of the passed-in arrays.

Arity: 2

Parameters:
  • array (list) – List to union with.
  • others (list) – Lists to unionize with array.
Returns:

Unionized list.

Return type:

list

Example

>>> union([1, 2, 3], [2, 3, 4])
[1, 2, 3, 4, 5]

New in version 1.0.0.

pydash.fp.arrays.union_by(*args)

This method is similar to union() except that it accepts iteratee which is invoked for each element of each arrays to generate the criterion by which uniqueness is computed.

Arity: 3

Parameters:
  • iteratee (function) – Function to invoke on each element.
  • array (list) – List to unionize with.
  • others (list) – Lists to unionize with array.
Returns:

Unionized list.

Return type:

list

Example

>>> union_by(lambda x: x % 2, [1, 2, 3], [2, 3, 4])
[1, 2]
>>> union_by(lambda x: x % 9, [1, 2, 3], [2, 3, 4])
[1, 2, 3, 4]

New in version 4.0.0.

pydash.fp.arrays.union_with(*args)

This method is like union() except that it accepts comparator which is invoked to compare elements of arrays. Result values are chosen from the first array in which the value occurs.

Arity: 3

Parameters:
  • comparator (callable) – Function to compare the elements of the arrays. Defaults to is_equal().
  • array (list) – List to unionize with.
  • others (list) – Lists to unionize with array.
Returns:

Unionized list.

Return type:

list

Example

>>> comparator = lambda a, b: a % 2 == b % 2
>>> union_with(comparator, [1, 2, 3], [2, 3, 4])
[1, 2]

New in version 4.0.0.

pydash.fp.arrays.uniq(array)[source]

Creates a duplicate-value-free version of the array. If iteratee is passed, each element of array is passed through a iteratee before uniqueness is 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.

Parameters:array (list) – List to process.
Returns:Unique list.
Return type:list

Example

>>> uniq([1, 2, 3, 1, 2, 3])
[1, 2, 3]

New in version 1.0.0.

    Changed in version 4.0.0:
  • Moved iteratee argument to uniq_by().

  • Removed alias unique.

pydash.fp.arrays.uniq_by(*args)

This method is like uniq() except that it accepts iteratee which is invoked for each element in array to generate the criterion by which uniqueness is computed. The order of result values is determined by the order they occur in the array. The iteratee is invoked with one argument: (value).

Arity: 2

Parameters:
  • iteratee (mixed) – Function to transform the elements of the arrays. Defaults to identity().
  • array (list) – List to process.
Returns:

Unique list.

Return type:

list

Example

>>> uniq_by(lambda val: val % 2, [1, 2, 3, 1, 2, 3])
[1, 2]

New in version 4.0.0.

pydash.fp.arrays.uniq_with(*args)

This method is like _.uniq except that it accepts comparator which is invoked to compare elements of array. The order of result values is determined by the order they occur in the array.The comparator is invoked with two arguments: (value, other).

Arity: 2

Parameters:
  • comparator (callable) – Function to compare the elements of the arrays. Defaults to is_equal().
  • array (list) – List to process.
Returns:

Unique list.

Return type:

list

Example

>>> uniq_with(lambda a, b: a % 2 == b % 2, [1, 2, 3, 4, 5])
[1, 2]

New in version 4.0.0.

pydash.fp.arrays.unzip(array)[source]

The inverse of zip_(), this method splits groups of elements into lists composed of elements from each group at their corresponding indexes.

Parameters:array (list) – List to process.
Returns:Unzipped list.
Return type:list

Example

>>> unzip([[1, 4, 7], [2, 5, 8], [3, 6, 9]])
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

New in version 1.0.0.

pydash.fp.arrays.unzip_with(*args)

This method is like unzip() except that it accepts a iteratee to specify how regrouped values should be combined. The iteratee is invoked with four arguments: (accumulator, value, index, group).

Arity: 2

Parameters:
  • iteratee (callable) – Function to combine regrouped values.
  • array (list) – List to process.
Returns:

Unzipped list.

Return type:

list

Example

>>> from pydash import add
>>> unzip_with(add, [[1, 10, 100], [2, 20, 200]])
[3, 30, 300]

New in version 3.3.0.

pydash.fp.arrays.without(*args)

Creates an array with all occurrences of the passed values removed.

Arity: 2

Parameters:
  • values (mixed) – Values to remove.
  • array (list) – List to filter.
Returns:

Filtered list.

Return type:

list

Note

The fp variant of without takes exactly 2 arguments

Example

>>> without(2, [1, 2, 3, 2, 4, 4])
[1, 3]

New in version 1.0.0.

pydash.fp.arrays.xor(*args)

Creates a list that is the symmetric difference of the provided lists.

Arity: 2

Parameters:
  • array (list) – List to process.
  • *lists (list) – Lists to xor with.
Returns:

XOR’d list.

Return type:

list

Example

>>> xor([1, 3, 4], [1, 2, 4])
[3]

New in version 1.0.0.

pydash.fp.arrays.xor_by(*args)

This method is like xor() except that it accepts iteratee which is invoked for each element of each arrays to generate the criterion by which by which they’re compared. The order of result values is determined by the order they occur in the arrays. The iteratee is invoked with one argument: (value).

Arity: 3

Parameters:
  • iteratee (mixed) – Function to transform the elements of the arrays. Defaults to identity().
  • array (list) – List to process.
  • *lists (list) – Lists to xor with.
Returns:

XOR’d list.

Return type:

list

Example

>>> xor_by(round, [2.1, 1.2], [2.3, 3.4])
[1.2, 3.4]
>>> xor_by('x', [{'x': 1}], [{'x': 2}, {'x': 1}])
[{'x': 2}]

New in version 4.0.0.

pydash.fp.arrays.xor_with(*args)

This method is like xor() except that it accepts comparator which is invoked to compare elements of arrays. The order of result values is determined by the order they occur in the arrays. The comparator is invoked with two arguments: (arr_val, oth_val).

Arity: 3

Parameters:
  • comparator (callable) – Function to compare the elements of the arrays. Defaults to is_equal().
  • array (list) – List to process.
  • *lists (list) – Lists to xor with.
Returns:

XOR’d list.

Return type:

list

Example

>>> objects = [{'x': 1, 'y': 2}, {'x': 2, 'y': 1}]
>>> others = [{'x': 1, 'y': 1}, {'x': 1, 'y': 2}]
>>> expected = [{'y': 1, 'x': 2}, {'y': 1, 'x': 1}]
>>> xor_with(lambda a, b: a == b, objects, others) == expected
True

New in version 4.0.0.

pydash.fp.arrays.zip_(*args)

Groups the elements of each array at their corresponding indexes. Useful for separate data sources that are coordinated through matching array indexes.

Arity: 2

Parameters:arrays (list) – Lists to process.
Returns:Zipped list.
Return type:list

Example

>>> zip_([1, 2, 3], [4, 5, 6], [7, 8, 9])
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]

New in version 1.0.0.

pydash.fp.arrays.zip_object(*args)

Creates a dict composed from lists of keys and values. Pass either a single two dimensional list, i.e. [[key1, value1], [key2, value2]], or two lists, one of keys and one of corresponding values.

Arity: 2

Parameters:
  • keys (list) – Either a list of keys or a list of [key, value] pairs.
  • values (list) – List of values to zip.
Returns:

Zipped dict.

Return type:

dict

Example

>>> zip_object([1, 2, 3], [4, 5, 6])
{1: 4, 2: 5, 3: 6}

New in version 1.0.0.

Changed in version 4.0.0: Removed alias object_.

pydash.fp.arrays.zip_object_deep(*args)

This method is like zip_object() except that it supports property paths.

Arity: 2

Parameters:
  • keys (list) – Either a list of keys or a list of [key, value] pairs.
  • values (list) – List of values to zip.
Returns:

Zipped dict.

Return type:

dict

Example

>>> expected = {'a': {'b': {'c': 1, 'd': 2}}}
>>> zip_object_deep(['a.b.c', 'a.b.d'], [1, 2]) == expected
True

New in version 4.0.0.

pydash.fp.arrays.zip_with(*args)

This method is like zip() except that it accepts a iteratee to specify how grouped values should be combined. The iteratee is invoked with four arguments: (accumulator, value, index, group).

Arity: 3

Parameters:
  • *arrays (list) – Lists to process.
  • iteratee (function) – Function to combine grouped values.
Returns:

Zipped list of grouped elements.

Return type:

list

Example

>>> from pydash import add

New in version 3.3.0.