Skip to content

Code Reference

Bases: Iterable[T]

Parameters:

Name Type Description Default
collection Iterable[T]

The collection to be queried.

required

Returns:

Name Type Description
None None

This method does not return any value.

Example
# Example Usage:
data = [1, 2, 3, 4, 5]
queryable_data = Queryable(data)

aggregate(func) ¤

Aggregates the elements of the sequence using a specified binary function.

Parameters:

Name Type Description Default
func Callable[[T, T], T]

A binary function that takes two elements of the sequence and returns a single aggregated result.

required

Returns:

Name Type Description
T T

The result of aggregating the elements using the specified function.

Raises:

Type Description
ValueError

If the sequence is empty and cannot be aggregated.

Example
# Example 1: Aggregating a list of numbers using the addition function
numbers = [1, 2, 3, 4, 5]
result = aggregate(numbers, lambda x, y: x + y)
print(result)  # Output: 15

# Example 2: Aggregating a list of strings using the concatenation function
words = ["Hello", " ", "World", "!"]
result = aggregate(words, lambda x, y: x + y)
print(result)  # Output: Hello World!

all(predicate) ¤

Determines whether all elements of the sequence satisfy a given predicate.

Parameters:

Name Type Description Default
predicate Callable[[T], bool]

The predicate function to apply to each element.

required

Returns:

Name Type Description
bool bool

True if all elements satisfy the predicate, False otherwise.

Example
# Example: Check if all numbers are even
numbers = Queryable([2, 4, 6, 8])
result = numbers.all(lambda x: x % 2 == 0)
print(result)  # Output: True

any(predicate=None) ¤

Determines whether any elements of the sequence satisfy a given predicate.

Parameters:

Name Type Description Default
predicate Callable[[T], bool]

The predicate function to apply to each element.

None

Returns:

Name Type Description
bool bool

True if any elements satisfy the predicate, False otherwise.

Example
# Example: Check if any numbers are odd
numbers = Queryable([2, 4, 6, 8])
result = numbers.any(lambda x: x % 2 != 0)
print(result)  # Output: False

average() ¤

Calculates the average of all elements in the sequence.

Returns:

Name Type Description
int int

The average of all elements in the sequence.

Example
# Example: Calculate the average of a list of numbers
numbers = Queryable([1, 2, 3, 4, 5])
result = numbers.average()
print(result)  # Output: 3

concat(other) ¤

Concatenates the elements of the current Queryable with the elements from another iterable.

Parameters:

Name Type Description Default
other Iterable[T]

Another iterable whose elements will be appended to the current Queryable.

required

Returns:

Type Description
Queryable[T]

Queryable[T]: A new Queryable containing the concatenated elements.

Example
# Create a Queryable with initial elements
queryable1 = Queryable([1, 2, 3])

# Another iterable to concatenate
other_iterable = [4, 5, 6]

# Concatenate the two iterables
result_queryable = queryable1.concat(other_iterable)

# Result Queryable contains elements from both iterables
assert list(result_queryable) == [1, 2, 3, 4, 5, 6]

contains(value) ¤

Determines whether the sequence contains a specific value.

Parameters:

Name Type Description Default
value T

The value to check for in the sequence.

required

Returns:

Name Type Description
T T

True if the sequence contains the value, False otherwise.

Example
# Example: Check if a list contains a specific element
numbers = Queryable([1, 2, 3, 4])
result = numbers.contains(3)
print(result)  # Output: True

count(predicate=None) ¤

Counts the number of elements in the sequence or those satisfying a given predicate.

Parameters:

Name Type Description Default
predicate Callable[[T], bool]

The predicate function to filter elements.

None

Returns:

Name Type Description
int int

The number of elements in the sequence or satisfying the predicate.

Example
# Example: Count the number of even numbers
numbers = Queryable([1, 2, 3, 4, 5, 6])
result = numbers.count(lambda x: x % 2 == 0)
print(result)  # Output: 3

default_if_empty(default) ¤

Returns a new Queryable with a default value if the sequence is empty.

Parameters:

Name Type Description Default
default T

The default value to include in the new Queryable if the sequence is empty.

required

Returns:

Type Description
Queryable[T]

Queryable[T]: A new Queryable containing the original elements or the default value.

Example
# Example: Provide a default value for an empty list
empty_list = Queryable([])
result = empty_list.default_if_empty(default=0).to_list()
print(result)  # Output: [0]

distinct() ¤

Returns a new Queryable containing distinct elements from the original Queryable.

Returns:

Name Type Description
Queryable Queryable[T]

A new Queryable with distinct elements.

Example
data = [1, 2, 2, 3, 4, 4, 5]
queryable_data = Queryable(data)

distinct_queryable = queryable_data.distinct()

# Result: Queryable([1, 2, 3, 4, 5])

element_at(index) ¤

Returns the element at the specified index in the sequence.

Parameters:

Name Type Description Default
index int

The index of the element to retrieve.

required

Returns:

Name Type Description
T T

The element at the specified index.

Raises:

Type Description
ValueError

If the sequence contains no element at the specified index.

Example
# Example: Get the element at index 2 in a list
numbers = Queryable([1, 2, 3, 4, 5])
result = numbers.element_at(2)
print(result)  # Output: 3

element_at_or_default(index, default=None) ¤

Returns the element at the specified index in the sequence, or a default value if none found.

Parameters:

Name Type Description Default
index int

The index of the element to retrieve.

required
default Optional[T]

The default value to return if no element is found at the specified index.

None

Returns:

Name Type Description
T T

The element at the specified index, or the default value if none found.

Example
# Example: Get the element at index 5 in a list or return -1 if none found
numbers = Queryable([1, 2, 3, 4, 5])
result = numbers.element_at_or_default(5, default=-1)
print(result)  # Output: -1

empty() classmethod ¤

Create an empty instance of the Queryable class.

This class method returns a new Queryable instance initialized with an empty list.

Returns:

Name Type Description
Queryable Queryable[T]

A new Queryable instance with an empty list.

Examples:

empty_queryable = Queryable.empty()
print(empty_queryable)  # Output: Queryable([])

except_for(other) ¤

Returns a new Queryable containing elements that are not in the specified sequence.

Parameters:

Name Type Description Default
other Iterable[T]

Another iterable sequence to exclude from the current sequence.

required

Returns:

Type Description
Queryable[T]

Queryable[T]: A new Queryable containing elements not present in the specified sequence.

Example
# Example: Exclude common elements from two sets of numbers
set1 = Queryable([1, 2, 3, 4])
set2 = [3, 4, 5, 6]
result = set1.except_for(set2)
print(result)  # Output: Queryable([1, 2])

first(predicate=None) ¤

Returns the first element of the sequence satisfying the optional predicate.

Parameters:

Name Type Description Default
predicate Optional[Callable[[T], bool]]

The optional predicate function to filter elements.

None

Returns:

Name Type Description
T T

The first element of the sequence satisfying the predicate.

Raises:

Type Description
ValueError

If the sequence is empty or no element satisfies the predicate.

Example
# Example: Find the first even number in a list
numbers = Queryable([1, 2, 3, 4, 5])
result = numbers.first(lambda x: x % 2 == 0)
print(result)  # Output: 2

first_or_default(predicate=None, default=None) ¤

Returns the first element of the sequence satisfying the optional predicate, or a default value.

Parameters:

Name Type Description Default
predicate Callable[[T], bool]

The optional predicate function to filter elements.

None
default Optional[T]

The default value to return if no element satisfies the predicate.

None

Returns:

Name Type Description
T T

The first element of the sequence satisfying the predicate, or the default value if none found.

Example
# Example: Find the first odd number in a list or return 0 if none found
numbers = Queryable([2, 4, 6, 8])
result = numbers.first_or_default(lambda x: x % 2 != 0, default=0)
print(result)  # Output: 0

group_join(inner, outer_key_selector, inner_key_selector, result_selector) ¤

Performs a group join operation between two sequences.

Parameters:

Name Type Description Default
inner Iterable[U]

The inner sequence to join with the outer sequence.

required
outer_key_selector Callable[[T], K]

A function to extract the key from elements in the outer sequence.

required
inner_key_selector Callable[[U], K]

A function to extract the key from elements in the inner sequence.

required
result_selector Callable[[T, Iterable[U]], V]

A function to create a result element from an outer element and its corresponding inner elements.

required

Returns:

Name Type Description
Queryable Queryable[T]

A new Queryable containing the result of the group join operation.

Example
# Example usage of group_join

# Define two sequences
outer_sequence = Queryable([1, 2, 3, 4])
inner_sequence = Queryable([(1, 'a'), (2, 'b'), (2, 'c'), (3, 'd')])

# Perform a group join based on the first element of each tuple
result = outer_sequence.group_join(
    inner_sequence,
    outer_key_selector=lambda x: x,
    inner_key_selector=lambda x: x[0],
    result_selector=lambda outer, inner: (outer, list(inner))
)

# Display the result
for item in result:
    print(item)

# Output:
# (1, [('a',)])
# (2, [('b', 'c')])
# (3, [('d',)])
# (4, [])

intersect(other) ¤

Returns a new Queryable containing common elements between two sequences.

Parameters:

Name Type Description Default
other Iterable[T]

Another iterable sequence to perform the intersection with.

required

Returns:

Type Description
Queryable[T]

Queryable[T]: A new Queryable containing common elements between both sequences.

Example
# Example: Intersection of two sets of numbers
set1 = Queryable([1, 2, 3, 4])
set2 = [3, 4, 5, 6]
result = set1.intersect(set2)
print(result)  # Output: Queryable([3, 4])

join(inner, outer_key_selector, inner_key_selector, result_selector) ¤

Joins two iterables based on key selectors and applies a result selector.

Parameters:

Name Type Description Default
inner Iterable[U]

The inner iterable to join with.

required
outer_key_selector Callable[[T], K]

The key selector for the outer sequence.

required
inner_key_selector Callable[[U], K]

The key selector for the inner sequence.

required
result_selector Callable[[T, U], V]

The result selector function to apply.

required

Returns:

Name Type Description
Queryable Queryable[V]

A new Queryable containing the joined elements based on the specified conditions.

Example
# Example: Join two sets of numbers based on common factors
set1 = Queryable([1, 2, 3, 4])
set2 = [3, 4, 5, 6]
result = set1.join(set2, outer_key_selector=lambda x: x, inner_key_selector=lambda x: x % 3,
                   result_selector=lambda x, y: (x, y))
print(result.to_list())  # Output: [(1, 4), (2, 5), (3, 6)]

last(predicate=None) ¤

Returns the last element of the sequence satisfying the optional predicate.

Parameters:

Name Type Description Default
predicate Optional[Callable[[T], bool]]

The optional predicate function to filter elements.

None

Returns:

Name Type Description
T T

The last element of the sequence satisfying the predicate.

Raises:

Type Description
ValueError

If the sequence is empty or no element satisfies the predicate.

Example
# Example: Find the last even number in a list
numbers = Queryable([1, 2, 3, 4, 5])
result = numbers.last(lambda x: x % 2 == 0)
print(result)  # Output: 4

last_or_default(predicate=None, default=None) ¤

Returns the last element of the sequence satisfying the optional predicate, or a default value.

Parameters:

Name Type Description Default
predicate Optional[Callable[[T], bool]]

The optional predicate function to filter elements.

None
default Optional[T]

The default value to return if no element satisfies the predicate.

None

Returns:

Name Type Description
T T

The last element of the sequence satisfying the predicate, or the default value if none found.

Example
# Example: Find the last odd number in a list or return 0 if none found
numbers = Queryable([2, 4, 6, 8])
result = numbers.last_or_default(lambda x: x % 2 != 0, default=0)
print(result)  # Output: 0

max() ¤

Finds the maximum value among the elements in the sequence.

Returns:

Name Type Description
int int

The maximum value in the sequence.

Example
# Example: Find the maximum value in a list of numbers
numbers = Queryable([3, 1, 4, 1, 5, 9, 2])
result = numbers.max()
print(result)  # Output: 9

min() ¤

Finds the minimum value among the elements in the sequence.

Returns:

Name Type Description
int int

The minimum value in the sequence.

Example
# Example: Find the minimum value in a list of numbers
numbers = Queryable([3, 1, 4, 1, 5, 9, 2])
result = numbers.min()
print(result)  # Output: 1

of_type(type_filter) ¤

Filters the elements of the Queryable to include only items of a specific type.

Parameters:

Name Type Description Default
type_filter type

The type to filter the elements by.

required

Returns:

Name Type Description
Queryable Queryable[T]

A new Queryable containing only elements of the specified type.

Example
# Create a Queryable with mixed types
data = [1, "two", 3.0, "four", 5]

# Create a Queryable instance
queryable_data = Queryable(data)

# Filter the Queryable to include only integers
result = queryable_data.of_type(int)

# Print the filtered result
print(result)  # Output: Queryable([1, 5])

order_by(key_selector) ¤

Orders the elements of the Queryable based on a key selector function.

Parameters:

Name Type Description Default
key_selector Callable[[T], U]

A function that takes an element of the Queryable and returns a value used for sorting.

required

Returns:

Name Type Description
Queryable Queryable[T]

A new Queryable containing the elements sorted based on the key selector.

Example
# Create a Queryable with a list of tuples
data = Queryable([(1, "apple"), (3, "banana"), (2, "orange")])

# Order the Queryable based on the first element of each tuple (numeric order)
result = data.order_by(lambda x: x[0]).to_list()

# Output: [(1, 'apple'), (2, 'orange'), (3, 'banana')]
print(result)

order_by_descending(key_selector) ¤

Orders the elements of the Queryable in descending order based on the specified key selector.

Parameters:

Name Type Description Default
key_selector Callable[[T], U]

A function that extracts a comparable key from each element.

required

Returns:

Name Type Description
Queryable Queryable[T]

A new Queryable with elements sorted in descending order.

Example
# Example usage of order_by_descending method
data = [5, 2, 8, 1, 7]
queryable_data = Queryable(data)

# Sorting the data in descending order based on the element itself
result = queryable_data.order_by_descending(lambda x: x)

# Result: Queryable([8, 7, 5, 2, 1])
# Another example with custom key selector
class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

people = [Person("Alice", 25), Person("Bob", 30), Person("Charlie", 22)]
queryable_people = Queryable(people)

# Sorting people by age in descending order
result = queryable_people.order_by_descending(lambda person: person.age)

# Result: Queryable([Person('Bob', 30), Person('Alice', 25), Person('Charlie', 22)])

range(start, stop=None, step=1) classmethod ¤

Create a Queryable instance representing a range of integers.

Parameters:

Name Type Description Default
start int

The starting value of the range.

required
stop Optional[int]

The end value (exclusive) of the range. If None, start is considered as the stop, and start is set to 0.

None
step int

The step between each pair of consecutive values in the range. Default is 1.

1

Returns:

Name Type Description
Queryable Queryable[int]

A Queryable instance representing the specified range of integers.

Example
result = Queryable.range(1, 5, 2)
print(result)
Queryable([1, 3])

result = Queryable.range(3)
print(result)
Queryable([0, 1, 2])

select(selector) ¤

Projects each element of the Queryable using the provided selector function.

Parameters:

Name Type Description Default
selector Callable[[T], U]

A function that maps elements of the Queryable to a new value.

required

Returns:

Name Type Description
Queryable Queryable[T]

A new Queryable containing the results of applying the selector function to each element.

Example
# Example usage of select method
def double(x):
    return x * 2

data = Queryable([1, 2, 3, 4, 5])
result = data.select(double)

# The 'result' Queryable will contain [2, 4, 6, 8, 10]

select_many(selector) ¤

Projects each element of the sequence to an iterable and flattens the resulting sequences into one sequence.

Parameters:

Name Type Description Default
selector Callable[[T], Iterable[U]]

A function that transforms each element of the sequence into an iterable.

required

Returns:

Name Type Description
Queryable Queryable[T]

A new Queryable instance containing the flattened sequence.

Example
# Example usage:
def get_digits(n: int) -> Iterable[int]:
    return (int(digit) for digit in str(n))

numbers = Queryable([123, 456, 789])
result = numbers.select_many(get_digits)

print(list(result))
# Output: [1, 2, 3, 4, 5, 6, 7, 8, 9]

single(predicate=None) ¤

Returns the single element of the sequence satisfying the optional predicate.

Parameters:

Name Type Description Default
predicate Callable[[T], bool]

The optional predicate function to filter elements.

None

Returns:

Name Type Description
T T

The single element of the sequence satisfying the predicate.

Raises:

Type Description
ValueError

If the sequence is empty, contains more than one element, or no element satisfies the predicate.

Example
# Example: Find the single even number in a list
numbers = Queryable([2, 4, 6, 8])
result = numbers.single(lambda x: x % 2 == 0)
print(result)  # Output: 2

single_or_default(predicate=None, default=None) ¤

Returns the single element of the sequence satisfying the optional predicate, or a default value if no such element is found.

Parameters:

Name Type Description Default
predicate Callable[[T], bool]

The optional predicate function to filter elements.

None
default Optional[T]

The default value to return if no element satisfies the predicate.

None

Returns:

Name Type Description
T T

The single element of the sequence satisfying the predicate, or the default value.

Raises:

Type Description
ValueError

If the sequence contains more than one element satisfying the predicate.

Example
# Example: Find the single odd number in a list or return 0 if none found
numbers = Queryable([2, 4, 6, 8])
result = numbers.single_or_default(lambda x: x % 2 != 0, default=0)
print(result)  # Output: 0

skip(count) ¤

Skips the specified number of elements from the beginning of the Queryable.

Parameters:

Name Type Description Default
count int

The number of elements to skip.

required

Returns:

Name Type Description
Queryable Queryable[T]

A new Queryable object containing the remaining elements after skipping.

Example
# Create a Queryable with elements [1, 2, 3, 4, 5]
queryable = Queryable([1, 2, 3, 4, 5])

# Skip the first 2 elements
result = queryable.skip(2)

# The result should contain elements [3, 4, 5]
assert list(result) == [3, 4, 5]

sum() ¤

Calculates the sum of all elements in the sequence.

Returns:

Name Type Description
int int

The sum of all elements in the sequence.

Example
# Example: Calculate the sum of a list of numbers
numbers = Queryable([1, 2, 3, 4, 5])
result = numbers.sum()
print(result)  # Output: 15

take(count) ¤

Returns a new Queryable containing the first 'count' elements of the current Queryable.

Parameters: - count (int): The number of elements to take from the current Queryable.

Returns: - Queryable: A new Queryable containing the first 'count' elements.

Example:

# Example usage of take method
data = Queryable([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
result = data.take(3)

print(result.to_list())  # Output: [1, 2, 3]

then_by(key_selector) ¤

Applies a secondary sorting to the elements of the Queryable based on the specified key_selector.

Parameters:

Name Type Description Default
key_selector Callable[[T], U]

A function that extracts a key from each element for sorting.

required

Returns:

Name Type Description
Queryable Queryable[T]

A new Queryable with the elements sorted first by the existing sorting criteria, and then by the specified key_selector.

Example
class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

people = [
    Person("Alice", 30),
    Person("Bob", 25),
    Person("Charlie", 35),
]

queryable_people = Queryable(people)

# Sort by age in ascending order and then by name in ascending order
sorted_people = queryable_people.order_by(lambda p: p.age).then_by(lambda p: p.name).to_list()

# Result: [Bob(25), Alice(30), Charlie(35)]

then_by_descending(key_selector) ¤

Sorts the elements of the Queryable in descending order based on the specified key selector.

Parameters:

Name Type Description Default
key_selector Callable[[T], U]

A function that takes an element of the Queryable and returns a value used for sorting.

required

Returns:

Name Type Description
Queryable Queryable[T]

A new Queryable with elements sorted in descending order based on the key selector.

Example

# Example usage of then_by_descending method
from typing import List

class Person:
    def __init__(self, name: str, age: int):
        self.name = name
        self.age = age

    def __repr__(self):
        return f"Person(name={self.name}, age={self.age})"

people: List[Person] = [
    Person("Alice", 30),
    Person("Bob", 25),
    Person("Charlie", 35),
]

# Create a Queryable from a list of Person objects
queryable_people = Queryable(people)

# Sort the people by age in descending order
sorted_people = queryable_people.then_by_descending(lambda person: person.age)

# Display the sorted list
print(sorted_people)
Output:
[Person(name=Charlie, age=35), Person(name=Alice, age=30), Person(name=Bob, age=25)]

to_dictionary(key_selector, value_selector=None) ¤

Converts the Queryable to a dictionary using key and optional value selectors.

Parameters:

Name Type Description Default
key_selector Callable[[T], K]

The key selector function.

required
value_selector Optional[Callable[[T], V]]

The optional value selector function.

None

Returns:

Type Description
dict[K, Union[V, T]]

dict[K, V]: A dictionary containing elements of the Queryable.

Example
# Example: Convert Queryable of tuples to a dictionary with the first element as the key
pairs = Queryable([(1, 'one'), (2, 'two'), (3, 'three')])
result = pairs.to_dictionary(key_selector=lambda x: x[0], value_selector=lambda x: x[1])
print(result)  # Output: {1: 'one', 2: 'two', 3: 'three'}

to_list() ¤

Converts the Queryable to a list.

Returns:

Type Description
list[T]

list[T]: A list containing all elements of the Queryable.

Example
# Example: Convert Queryable to a list
numbers = Queryable([1, 2, 3, 4, 5])
result = numbers.to_list()
print(result)  # Output: [1, 2, 3, 4, 5]

union(other) ¤

Returns a new Queryable containing unique elements from both sequences.

Parameters:

Name Type Description Default
other Iterable[T]

Another iterable sequence to perform the union with.

required

Returns:

Type Description
Queryable[T]

Queryable[T]: A new Queryable containing unique elements from both sequences.

Example
# Example: Union of two sets of numbers
set1 = Queryable([1, 2, 3, 4])
set2 = [3, 4, 5, 6]
result = set1.union(set2)
print(result)  # Output: Queryable([1, 2, 3, 4, 5, 6])

where(predicate) ¤

Filters the elements of the Queryable based on a given predicate.

Parameters:

Name Type Description Default
predicate Callable[[T], bool]

A function that takes an element of the Queryable and returns a boolean indicating whether the element should be included in the result.

required

Returns:

Name Type Description
Queryable Queryable[T]

A new Queryable containing the elements that satisfy the given predicate.

Example
# Create a Queryable with numbers from 1 to 5
numbers = Queryable([1, 2, 3, 4, 5])

# Define a predicate to filter even numbers
def is_even(n):
    return n % 2 == 0

# Use the 'where' method to filter even numbers
result = numbers.where(is_even)

# Display the result
print(list(result))
# Output: [2, 4]

zip(other) ¤

Zips the elements of the current Queryable instance with the elements of another iterable.

Parameters:

Name Type Description Default
other Iterable[T]

The iterable to zip with the current Queryable.

required

Returns:

Type Description
Queryable[T]

Queryable[T]: A new Queryable instance containing tuples of zipped elements.

Example
queryable1 = Queryable([1, 2, 3, 4])
queryable2 = Queryable(['a', 'b', 'c', 'd'])
result = queryable1.zip(queryable2)

list(result)
[(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd')]