CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-pydash

A comprehensive Python utility library for functional programming inspired by JavaScript's Lo-Dash

Pending
Overview
Eval results
Files

arrays.mddocs/

Arrays Module

The Arrays module provides 75 functions for manipulating arrays and sequences. These functions cover chunking, filtering, set operations, transformations, and various array utilities.

Core Array Functions

chunk

def chunk(array: Sequence[T], size: int = 1) -> List[Sequence[T]]

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.

Parameters:

  • array (Sequence[T]): List to chunk.
  • size (int): Chunk size. Defaults to 1.

Returns:

  • List[Sequence[T]]: New list containing chunks of array.

Example:

from pydash import chunk

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

chunk([1, 2, 3, 4, 5, 6], 3)
# [[1, 2, 3], [4, 5, 6]]

compact

def compact(array: Iterable[Union[T, None]]) -> List[T]

Creates a list with all falsey values of array removed.

Parameters:

  • array (Iterable[Union[T, None]]): List to compact.

Returns:

  • List[T]: Compacted list.

Example:

from pydash import compact

compact(["", 1, 0, True, False, None])
# [1, True]

compact([0, 1, False, 2, '', 3, None])
# [1, 2, 3]

concat

def concat(*arrays: Iterable[T]) -> List[T]

Concatenates zero or more lists into one.

Parameters:

  • arrays (*Iterable[T]): Lists to concatenate.

Returns:

  • List[T]: Concatenated list.

Example:

from pydash import concat

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

difference

def difference(array: Iterable[T], *others: Iterable[T]) -> List[T]

Creates a list of list elements not present in others.

Parameters:

  • array (Iterable[T]): List to process.
  • others (*Iterable[T]): Lists to check.

Returns:

  • List[T]: Difference between others.

Example:

from pydash import difference

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

difference([1, 2, 3, 4], [2, 3])
# [1, 4]

difference_by

def difference_by(array: Iterable[T], *others, iteratee=None) -> List[T]

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.

Parameters:

  • array (Iterable[T]): The array to find the difference of.
  • others (*Iterable[T]): Lists to check for difference with array.
  • iteratee (Callable, optional): Function to transform the elements.

Returns:

  • List[T]: Difference between others.

Example:

from pydash import difference_by

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

difference_with

def difference_with(array: Iterable[T], *others, comparator=None) -> List[T]

Like difference except that it accepts a comparator which is invoked to compare the elements of all arrays.

Parameters:

  • array (Iterable[T]): The array to find the difference of.
  • others (*Iterable[T]): Lists to check for difference with array.
  • comparator (Callable, optional): Function to compare elements.

Returns:

  • List[T]: Difference between others.

Example:

from pydash import difference_with

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

Array Slice Operations

drop

def drop(array: Sequence[T], n: int = 1) -> List[T]

Creates a slice with n elements dropped from the beginning.

Parameters:

  • array (Sequence[T]): Array to process.
  • n (int): Number of elements to drop. Defaults to 1.

Returns:

  • List[T]: Slice of array with n elements dropped from beginning.

Example:

from pydash import drop

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

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

drop_right

def drop_right(array: Sequence[T], n: int = 1) -> List[T]

Creates a slice with n elements dropped from the end.

Parameters:

  • array (Sequence[T]): Array to process.
  • n (int): Number of elements to drop. Defaults to 1.

Returns:

  • List[T]: Slice of array with n elements dropped from end.

Example:

from pydash import drop_right

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

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

drop_right_while

def drop_right_while(array: Sequence[T], predicate=None) -> List[T]

Creates a slice of array excluding elements dropped from the end while predicate returns truthy.

Parameters:

  • array (Sequence[T]): Array to process.
  • predicate (Callable, optional): Function invoked per iteration.

Returns:

  • List[T]: Slice of array.

Example:

from pydash import drop_right_while

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

drop_while

def drop_while(array: Sequence[T], predicate=None) -> List[T]

Creates a slice of array excluding elements dropped from the beginning while predicate returns truthy.

Parameters:

  • array (Sequence[T]): Array to process.
  • predicate (Callable, optional): Function invoked per iteration.

Returns:

  • List[T]: Slice of array.

Example:

from pydash import drop_while

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

take

def take(array: Sequence[T], n: int = 1) -> Sequence[T]

Creates a slice with n elements taken from the beginning.

Parameters:

  • array (Sequence[T]): Array to process.
  • n (int): Number of elements to take. Defaults to 1.

Returns:

  • Sequence[T]: Slice of array with n elements taken from beginning.

Example:

from pydash import take

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

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

take_right

def take_right(array: Sequence[T], n: int = 1) -> Sequence[T]

Creates a slice with n elements taken from the end.

Parameters:

  • array (Sequence[T]): Array to process.
  • n (int): Number of elements to take. Defaults to 1.

Returns:

  • Sequence[T]: Slice of array with n elements taken from end.

Example:

from pydash import take_right

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

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

take_right_while

def take_right_while(array: Sequence[T], predicate=None) -> Sequence[T]

Creates a slice of array with elements taken from the end while predicate returns truthy.

Parameters:

  • array (Sequence[T]): Array to process.
  • predicate (Callable, optional): Function invoked per iteration.

Returns:

  • Sequence[T]: Slice of array.

Example:

from pydash import take_right_while

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

take_while

def take_while(array: Sequence[T], predicate=None) -> List[T]

Creates a slice of array with elements taken from the beginning while predicate returns truthy.

Parameters:

  • array (Sequence[T]): Array to process.
  • predicate (Callable, optional): Function invoked per iteration.

Returns:

  • List[T]: Slice of array.

Example:

from pydash import take_while

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

Array Element Access

head

def head(array: Sequence[T]) -> Union[T, None]

Gets the first element of array.

Parameters:

  • array (Sequence[T]): Array to get first element of.

Returns:

  • Union[T, None]: First element of array.

Example:

from pydash import head

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

head([])
# None

last

def last(array: Sequence[T]) -> Union[T, None]

Gets the last element of array.

Parameters:

  • array (Sequence[T]): Array to get last element of.

Returns:

  • Union[T, None]: Last element of array.

Example:

from pydash import last

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

last([])
# None

nth

def nth(array: Iterable[T], pos: int = 0) -> Union[T, None]

Gets the element at index pos of array. If pos is negative, the nth element from the end is returned.

Parameters:

  • array (Iterable[T]): Array to get element from.
  • pos (int): Index of element to get. Defaults to 0.

Returns:

  • Union[T, None]: Element at index pos.

Example:

from pydash import nth

nth([1, 2, 3, 4], 1)
# 2

nth([1, 2, 3, 4], -1)
# 4

initial

def initial(array: Sequence[T]) -> Sequence[T]

Gets all but the last element of array.

Parameters:

  • array (Sequence[T]): Array to process.

Returns:

  • Sequence[T]: All but the last element of array.

Example:

from pydash import initial

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

tail

def tail(array: Sequence[T]) -> Sequence[T]

Gets all but the first element of array.

Parameters:

  • array (Sequence[T]): Array to process.

Returns:

  • Sequence[T]: All but the first element of array.

Example:

from pydash import tail

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

Array Search Functions

find_index

def find_index(array: Iterable[T], predicate=None) -> int

Returns the index of the first element that passes the predicate check, else -1.

Parameters:

  • array (Iterable[T]): Array to search.
  • predicate (Callable, optional): Function invoked per iteration.

Returns:

  • int: Index of found element, else -1.

Example:

from pydash import find_index

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

find_index([{'a': 1}, {'a': 2}], {'a': 1})
# 0

find_last_index

def find_last_index(array: Iterable[T], predicate=None) -> int

Like find_index except that it iterates from right to left.

Parameters:

  • array (Iterable[T]): Array to search.
  • predicate (Callable, optional): Function invoked per iteration.

Returns:

  • int: Index of found element, else -1.

Example:

from pydash import find_last_index

find_last_index([1, 2, 3, 4], lambda x: x > 2)
# 3

index_of

def index_of(array: Sequence[T], value: T, from_index: int = 0) -> int

Gets the index at which the first occurrence of value is found in array using SameValueZero for equality comparisons.

Parameters:

  • array (Sequence[T]): Array to search.
  • value (T): Value to search for.
  • from_index (int): Index to search from. Defaults to 0.

Returns:

  • int: Index of value in array, else -1.

Example:

from pydash import index_of

index_of([1, 2, 1, 2], 2)
# 1

index_of([1, 2, 1, 2], 2, 2)
# 3

last_index_of

def last_index_of(array: Sequence[T], value: T, from_index: int = None) -> int

Gets the index at which the last occurrence of value is found in array using SameValueZero for equality comparisons.

Parameters:

  • array (Sequence[T]): Array to search.
  • value (T): Value to search for.
  • from_index (int, optional): Index to search from.

Returns:

  • int: Index of value in array, else -1.

Example:

from pydash import last_index_of

last_index_of([1, 2, 1, 2], 2)
# 3

Array Flattening

flatten

def flatten(array: Iterable[Any]) -> List[Any]

Flattens array a single level deep.

Parameters:

  • array (Iterable[Any]): Array to flatten.

Returns:

  • List[Any]: Flattened array.

Example:

from pydash import flatten

flatten([1, [2, [3, [4]], 5]])
# [1, 2, [3, [4]], 5]

flatten_deep

def flatten_deep(array: Iterable[Any]) -> List[Any]

Recursively flattens array.

Parameters:

  • array (Iterable[Any]): Array to flatten.

Returns:

  • List[Any]: Flattened array.

Example:

from pydash import flatten_deep

flatten_deep([1, [2, [3, [4]], 5]])
# [1, 2, 3, 4, 5]

flatten_depth

def flatten_depth(array: Iterable[Any], depth: int = 1) -> List[Any]

Recursively flatten array up to depth times.

Parameters:

  • array (Iterable[Any]): Array to flatten.
  • depth (int): Maximum recursion depth. Defaults to 1.

Returns:

  • List[Any]: Flattened array.

Example:

from pydash import flatten_depth

flatten_depth([1, [2, [3, [4]], 5]], 1)
# [1, 2, [3, [4]], 5]

flatten_depth([1, [2, [3, [4]], 5]], 2)
# [1, 2, 3, [4], 5]

Set Operations

intersection

def intersection(array: Sequence[T], *others: Iterable[Any]) -> List[T]

Creates a list of unique values that are included in all given arrays using SameValueZero for equality comparisons.

Parameters:

  • array (Sequence[T]): Array to inspect.
  • others (*Iterable[Any]): Arrays to include values from.

Returns:

  • List[T]: New array of intersecting values.

Example:

from pydash import intersection

intersection([2, 1], [2, 3])
# [2]

intersection([2, 1], [4, 2], [1, 2])
# [2]

intersection_by

def intersection_by(array: Sequence[T], *others, iteratee=None) -> List[T]

Like intersection except that it accepts an iteratee which is invoked for each element of each array.

Parameters:

  • array (Sequence[T]): Array to inspect.
  • others (*Iterable[Any]): Arrays to include values from.
  • iteratee (Callable, optional): Function to transform elements.

Returns:

  • List[T]: New array of intersecting values.

Example:

from pydash import intersection_by
import math

intersection_by([2.1, 1.2], [2.3, 3.4], math.floor)
# [2.1]

intersection_with

def intersection_with(array: Sequence[T], *others, comparator=None) -> List[T]

Like intersection except that it accepts a comparator which is invoked to compare elements.

Parameters:

  • array (Sequence[T]): Array to inspect.
  • others (*Iterable[Any]): Arrays to include values from.
  • comparator (Callable, optional): Function to compare elements.

Returns:

  • List[T]: New array of intersecting values.

union

def union(array: Sequence[T], *others: Sequence[T]) -> List[Union[T, T2]]

Creates a list of unique values from all given arrays using SameValueZero for equality comparisons.

Parameters:

  • array (Sequence[T]): Array to inspect.
  • others (*Sequence[T]): Arrays to include values from.

Returns:

  • List[Union[T, T2]]: New array of combined values.

Example:

from pydash import union

union([2], [1, 2])
# [2, 1]

union_by

def union_by(array: Sequence[T], *others, iteratee=None) -> List[T]

Like union except that it accepts an iteratee which is invoked for each element of each array.

Parameters:

  • array (Sequence[T]): Array to inspect.
  • others (*Sequence[T]): Arrays to include values from.
  • iteratee (Callable, optional): Function to transform elements.

Returns:

  • List[T]: New array of combined values.

union_with

def union_with(array: Sequence[T], *others, comparator=None) -> List[T]

Like union except that it accepts a comparator which is invoked to compare elements.

Parameters:

  • array (Sequence[T]): Array to inspect.
  • others (*Sequence[T]): Arrays to include values from.
  • comparator (Callable, optional): Function to compare elements.

Returns:

  • List[T]: New array of combined values.

xor

def xor(array: Iterable[T], *lists: Iterable[T]) -> List[T]

Creates a list of unique values that is the symmetric difference of the given arrays.

Parameters:

  • array (Iterable[T]): Array to inspect.
  • lists (*Iterable[T]): Arrays to find symmetric difference with.

Returns:

  • List[T]: New array of symmetric differences.

Example:

from pydash import xor

xor([2, 1], [2, 3])
# [1, 3]

xor_by

def xor_by(array: Iterable[T], *lists, iteratee=None) -> List[T]

Like xor except that it accepts an iteratee which is invoked for each element.

Parameters:

  • array (Iterable[T]): Array to inspect.
  • lists (*Iterable[T]): Arrays to find symmetric difference with.
  • iteratee (Callable, optional): Function to transform elements.

Returns:

  • List[T]: New array of symmetric differences.

xor_with

def xor_with(array: Iterable[T], *lists, comparator=None) -> List[T]

Like xor except that it accepts a comparator which is invoked to compare elements.

Parameters:

  • array (Iterable[T]): Array to inspect.
  • lists (*Iterable[T]): Arrays to find symmetric difference with.
  • comparator (Callable, optional): Function to compare elements.

Returns:

  • List[T]: New array of symmetric differences.

Array Transformation

zip_

def zip_(*arrays: Iterable[Any]) -> List[Tuple[Any, ...]]

Creates a list of grouped elements, the first of which contains the first elements of the given arrays.

Parameters:

  • arrays (*Iterable[Any]): Arrays to process.

Returns:

  • List[Tuple[Any, ...]]: New array of grouped elements.

Example:

from pydash import zip_

zip_(['a', 'b'], [1, 2], [True, False])
# [('a', 1, True), ('b', 2, False)]

zip_object

def zip_object(keys: Iterable[T], values: List[T2] = None) -> Dict[T, T2]

Creates a dictionary composed from arrays of keys and values.

Parameters:

  • keys (Iterable[T]): Keys array.
  • values (List[T2], optional): Values array.

Returns:

  • Dict[T, T2]: New dictionary.

Example:

from pydash import zip_object

zip_object(['a', 'b'], [1, 2])
# {'a': 1, 'b': 2}

zip_object_deep

def zip_object_deep(keys: Iterable[Any], values: List[Any] = None) -> Dict[Any, Any]

Like zip_object except that it supports property paths.

Parameters:

  • keys (Iterable[Any]): Keys array (can be property paths).
  • values (List[Any], optional): Values array.

Returns:

  • Dict[Any, Any]: New dictionary.

Example:

from pydash import zip_object_deep

zip_object_deep(['a.b[0].c', 'a.b[1].d'], [1, 2])
# {'a': {'b': [{'c': 1}, {'d': 2}]}}

zip_with

def zip_with(*arrays, iteratee=None) -> List[Any]

Like zip_ except that it accepts an iteratee to specify how grouped values should be combined.

Parameters:

  • arrays (*Iterable[Any]): Arrays to process.
  • iteratee (Callable, optional): Function to combine grouped values.

Returns:

  • List[Any]: New array of grouped elements.

Example:

from pydash import zip_with

zip_with([1, 2], [10, 20], [100, 200], lambda a, b, c: a + b + c)
# [111, 222]

from_pairs

def from_pairs(pairs: Iterable[Tuple[T, T2]]) -> Dict[T, T2]

Creates a dictionary from a list of key-value pairs.

Parameters:

  • pairs (Iterable[Tuple[T, T2]]): Key-value pairs.

Returns:

  • Dict[T, T2]: New dictionary.

Example:

from pydash import from_pairs

from_pairs([['a', 1], ['b', 2]])
# {'a': 1, 'b': 2}

unzip

def unzip(array: Iterable[Iterable[Any]]) -> List[Tuple[Any, ...]]

The inverse of zip_. Creates a list of arrays regrouping the elements to their pre-zip configuration.

Parameters:

  • array (Iterable[Iterable[Any]]): Array of grouped elements.

Returns:

  • List[Tuple[Any, ...]]: New array of regrouped elements.

Example:

from pydash import unzip

unzip([('a', 1, True), ('b', 2, False)])
# [('a', 'b'), (1, 2), (True, False)]

unzip_with

def unzip_with(array: Iterable[Iterable[Any]], iteratee=None) -> List[Any]

Like unzip except that it accepts an iteratee to specify how regrouped values should be combined.

Parameters:

  • array (Iterable[Iterable[Any]]): Array of grouped elements.
  • iteratee (Callable, optional): Function to combine regrouped values.

Returns:

  • List[Any]: New array of regrouped elements.

Array Modification

fill

def fill(array: MutableSequence[Any], value: Any, start: int = 0, end: int = None) -> MutableSequence[Any]

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

Parameters:

  • array (MutableSequence[Any]): Array to fill.
  • value (Any): Value to fill array with.
  • start (int): Start position. Defaults to 0.
  • end (int, optional): End position.

Returns:

  • MutableSequence[Any]: array.

Example:

from pydash import fill

array = [1, 2, 3]
fill(array, 'a')
# ['a', 'a', 'a']

array = [1, 2, 3]
fill(array, '*', 1, 3)
# [1, '*', '*']

pull

def pull(array: List[T], *values: T) -> List[T]

Removes all provided values from array using SameValueZero for equality comparisons.

Parameters:

  • array (List[T]): Array to modify.
  • values (*T): Values to remove.

Returns:

  • List[T]: array.

Example:

from pydash import pull

array = ['a', 'b', 'c', 'a', 'b', 'c']
pull(array, 'a', 'c')
# ['b', 'b']

pull_all

def pull_all(array: List[T], values: Iterable[T]) -> List[T]

Like pull except that it accepts an array of values to remove.

Parameters:

  • array (List[T]): Array to modify.
  • values (Iterable[T]): Values to remove.

Returns:

  • List[T]: array.

Example:

from pydash import pull_all

array = ['a', 'b', 'c', 'a', 'b', 'c']
pull_all(array, ['a', 'c'])
# ['b', 'b']

pull_all_by

def pull_all_by(array: List[T], values: Iterable[T], iteratee=None) -> List[T]

Like pull_all except that it accepts an iteratee which is invoked for each element of array and values to generate the criterion by which they're compared.

Parameters:

  • array (List[T]): Array to modify.
  • values (Iterable[T]): Values to remove.
  • iteratee (Callable, optional): Function to transform elements.

Returns:

  • List[T]: array.

pull_all_with

def pull_all_with(array: List[T], values: Iterable[T], comparator=None) -> List[T]

Like pull_all except that it accepts a comparator which is invoked to compare elements of array to values.

Parameters:

  • array (List[T]): Array to modify.
  • values (Iterable[T]): Values to remove.
  • comparator (Callable, optional): Function to compare elements.

Returns:

  • List[T]: array.

pull_at

def pull_at(array: List[T], *indexes: int) -> List[T]

Removes elements from array corresponding to the given indexes and returns a list of the removed elements.

Parameters:

  • array (List[T]): Array to modify.
  • indexes (*int): Indexes of elements to remove.

Returns:

  • List[T]: New array of removed elements.

Example:

from pydash import pull_at

array = ['a', 'b', 'c', 'd']
pulled = pull_at(array, 1, 3)
# array is now ['a', 'c']
# pulled is ['b', 'd']

push

def push(array: List[T], *items: T2) -> List[Union[T, T2]]

Appends items to array.

Parameters:

  • array (List[T]): Array to modify.
  • items (*T2): Items to append.

Returns:

  • List[Union[T, T2]]: array.

Example:

from pydash import push

array = [1, 2]
push(array, 3, 4)
# [1, 2, 3, 4]

pop

def pop(array: List[T], index: int = -1) -> T

Removes and returns the element at index from array.

Parameters:

  • array (List[T]): Array to modify.
  • index (int): Index of element to remove. Defaults to -1.

Returns:

  • T: Removed element.

Example:

from pydash import pop

array = [1, 2, 3]
pop(array)
# 3
# array is now [1, 2]

shift

def shift(array: List[T]) -> T

Removes and returns the first element from array.

Parameters:

  • array (List[T]): Array to modify.

Returns:

  • T: Removed element.

Example:

from pydash import shift

array = [1, 2, 3]
shift(array)
# 1
# array is now [2, 3]

unshift

def unshift(array: List[T], *items: T2) -> List[Union[T, T2]]

Prepends items to the front of array.

Parameters:

  • array (List[T]): Array to modify.
  • items (*T2): Items to prepend.

Returns:

  • List[Union[T, T2]]: array.

Example:

from pydash import unshift

array = [3, 4]
unshift(array, 1, 2)
# [1, 2, 3, 4]

remove

def remove(array: List[T], predicate=None) -> List[T]

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

Parameters:

  • array (List[T]): Array to modify.
  • predicate (Callable, optional): Function invoked per iteration.

Returns:

  • List[T]: New array of removed elements.

Example:

from pydash import remove

array = [1, 2, 3, 4]
evens = remove(array, lambda x: x % 2 == 0)
# array is now [1, 3]
# evens is [2, 4]

reverse

def reverse(array: SequenceT) -> SequenceT

Reverses array so that the first element becomes the last, the second element becomes the second to last, and so on.

Parameters:

  • array (SequenceT): Array to reverse.

Returns:

  • SequenceT: array.

Example:

from pydash import reverse

array = [1, 2, 3]
reverse(array)
# [3, 2, 1]

slice_

def slice_(array: SequenceT, start: int = 0, end: Union[int, None] = None) -> SequenceT

Creates a slice of array from start up to, but not including, end.

Parameters:

  • array (SequenceT): Array to slice.
  • start (int): Start position. Defaults to 0.
  • end (Union[int, None]): End position.

Returns:

  • SequenceT: Slice of array.

Example:

from pydash import slice_

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

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

splice

def splice(array: List[T], start: int, count: int = None, *items: T2) -> List[T]

Changes the contents of array by removing existing elements and/or adding new elements.

Parameters:

  • array (List[T]): Array to modify.
  • start (int): Start index.
  • count (int, optional): Number of elements to remove.
  • items (*T2): Items to insert.

Returns:

  • List[T]: List of removed elements.

Example:

from pydash import splice

array = [1, 2, 3, 4]
removed = splice(array, 1, 2, 'a', 'b')
# array is now [1, 'a', 'b', 4]
# removed is [2, 3]

Array Utilities

duplicates

def duplicates(array: Iterable[T]) -> List[T]

Creates a unique list of duplicate values from array.

Parameters:

  • array (Iterable[T]): Array to inspect.

Returns:

  • List[T]: New array of duplicate values.

Example:

from pydash import duplicates

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

mapcat

def mapcat(array: Iterable[T], iteratee=None) -> List[T2]

Maps iteratee over array and concatenates/flattens the result.

Parameters:

  • array (Iterable[T]): Array to iterate over.
  • iteratee (Callable, optional): Function invoked per iteration.

Returns:

  • List[T2]: New concatenated array.

Example:

from pydash import mapcat

mapcat([1, 2], lambda x: [x, x])
# [1, 1, 2, 2]

sort

def sort(array: List[T], comparator=None, key=None, reverse=False) -> List[T]

Sorts array in-place using optional comparator, key function, or reverse flag.

Parameters:

  • array (List[T]): Array to sort.
  • comparator (Callable, optional): Comparison function.
  • key (Callable, optional): Key function.
  • reverse (bool): Whether to reverse sort order. Defaults to False.

Returns:

  • List[T]: array.

Example:

from pydash import sort

array = [3, 1, 2]
sort(array)
# [1, 2, 3]

split_at

def split_at(array: Sequence[T], index: int) -> List[Sequence[T]]

Splits array into two arrays: one with elements up to but not including index and another with the remaining elements.

Parameters:

  • array (Sequence[T]): Array to split.
  • index (int): Index to split at.

Returns:

  • List[Sequence[T]]: List of two split arrays.

Example:

from pydash import split_at

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

uniq

def uniq(array: Iterable[T]) -> List[T]

Creates a duplicate-free version of an array using SameValueZero for equality comparisons.

Parameters:

  • array (Iterable[T]): Array to inspect.

Returns:

  • List[T]: New duplicate free array.

Example:

from pydash import uniq

uniq([2, 1, 2])
# [2, 1]

uniq_by

def uniq_by(array: Iterable[T], iteratee=None) -> List[T]

Like uniq except that it accepts an iteratee which is invoked for each element in array to generate the criterion by which uniqueness is computed.

Parameters:

  • array (Iterable[T]): Array to inspect.
  • iteratee (Callable, optional): Function to transform elements.

Returns:

  • List[T]: New duplicate free array.

Example:

from pydash import uniq_by
import math

uniq_by([2.1, 1.2, 2.3], math.floor)
# [2.1, 1.2]

uniq_with

def uniq_with(array: Iterable[T], comparator=None) -> List[T]

Like uniq except that it accepts a comparator which is invoked to compare elements of array.

Parameters:

  • array (Iterable[T]): Array to inspect.
  • comparator (Callable, optional): Function to compare elements.

Returns:

  • List[T]: New duplicate free array.

without

def without(array: Iterable[T], *values: T) -> List[T]

Creates an array excluding all given values using SameValueZero for equality comparisons.

Parameters:

  • array (Iterable[T]): Array to inspect.
  • values (*T): Values to exclude.

Returns:

  • List[T]: New array of filtered values.

Example:

from pydash import without

without([2, 1, 2, 3], 1, 2)
# [3]

Sorted Array Functions

sorted_index

def sorted_index(array: Sequence[Any], value: Any) -> int

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

Parameters:

  • array (Sequence[Any]): Sorted array to inspect.
  • value (Any): Value to evaluate.

Returns:

  • int: Index at which value should be inserted.

Example:

from pydash import sorted_index

sorted_index([30, 50], 40)
# 1

sorted_index_by

def sorted_index_by(array: Sequence[T], value: T, iteratee=None) -> int

Like sorted_index except that it accepts an iteratee which is invoked for value and each element of array to compute their sort ranking.

Parameters:

  • array (Sequence[T]): Sorted array to inspect.
  • value (T): Value to evaluate.
  • iteratee (Callable, optional): Function to transform values.

Returns:

  • int: Index at which value should be inserted.

sorted_index_of

def sorted_index_of(array: Sequence[Any], value: Any) -> int

Like index_of except that it performs a binary search on a sorted array.

Parameters:

  • array (Sequence[Any]): Sorted array to inspect.
  • value (Any): Value to search for.

Returns:

  • int: Index of value, else -1.

sorted_last_index

def sorted_last_index(array: Sequence[Any], value: Any) -> int

Like sorted_index except that it returns the highest index at which value should be inserted.

Parameters:

  • array (Sequence[Any]): Sorted array to inspect.
  • value (Any): Value to evaluate.

Returns:

  • int: Index at which value should be inserted.

sorted_last_index_by

def sorted_last_index_by(array: Sequence[T], value: T, iteratee=None) -> int

Like sorted_last_index except that it accepts an iteratee which is invoked for value and each element of array.

Parameters:

  • array (Sequence[T]): Sorted array to inspect.
  • value (T): Value to evaluate.
  • iteratee (Callable, optional): Function to transform values.

Returns:

  • int: Index at which value should be inserted.

sorted_last_index_of

def sorted_last_index_of(array: Sequence[Any], value: Any) -> int

Like last_index_of except that it performs a binary search on a sorted array.

Parameters:

  • array (Sequence[Any]): Sorted array to inspect.
  • value (Any): Value to search for.

Returns:

  • int: Index of value, else -1.

sorted_uniq

def sorted_uniq(array: Iterable[SupportsRichComparisonT]) -> List[SupportsRichComparisonT]

Like uniq except that it's designed and optimized for sorted arrays.

Parameters:

  • array (Iterable[SupportsRichComparisonT]): Sorted array to inspect.

Returns:

  • List[SupportsRichComparisonT]: New duplicate free array.

sorted_uniq_by

def sorted_uniq_by(array: Sequence[T], iteratee=None) -> List[T]

Like uniq_by except that it's designed and optimized for sorted arrays.

Parameters:

  • array (Sequence[T]): Sorted array to inspect.
  • iteratee (Callable, optional): Function to transform elements.

Returns:

  • List[T]: New duplicate free array.

Advanced Array Operations

intercalate

def intercalate(array: Iterable[Any], separator: Any) -> List[Any]

Like intersperse but automatically flattens the result.

Parameters:

  • array (Iterable[Any]): Array to intercalate.
  • separator (Any): Separator to insert.

Returns:

  • List[Any]: Intercalated array.

Example:

from pydash import intercalate

intercalate([1, [2], [3]], 0)
# [1, 0, 2, 0, 3]

interleave

def interleave(*arrays: Iterable[T]) -> List[T]

Merges multiple arrays by round-robin insertion of each array's elements.

Parameters:

  • arrays (*Iterable[T]): Arrays to interleave.

Returns:

  • List[T]: Interleaved array.

Example:

from pydash import interleave

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

intersperse

def intersperse(array: Iterable[T], separator: T2) -> List[Union[T, T2]]

Inserts a separator between each element of array.

Parameters:

  • array (Iterable[T]): Array to modify.
  • separator (T2): Separator to insert.

Returns:

  • List[Union[T, T2]]: Interspersed array.

Example:

from pydash import intersperse

intersperse([1, 2, 3], 0)
# [1, 0, 2, 0, 3]

This module provides comprehensive array manipulation capabilities with 76 functions covering all aspects of array processing from basic operations to complex transformations and set operations.

Install with Tessl CLI

npx tessl i tessl/pypi-pydash

docs

arrays.md

chaining.md

collections.md

functions.md

index.md

numerical.md

objects.md

predicates.md

strings.md

utilities.md

tile.json