Skip to content

Joins and lookups

The examples join people to their orders. Each item is a plain tuple:

people = [("Ada", 1), ("Tim", 2), ("Grace", 3)]   # (name, id)
orders = [(10, 1), (11, 1), (12, 4)]              # (order_id, person_id)

Join

join keeps only the pairs that match (.NET Join). The arguments are the inner sequence, a key selector for each side, and a result selector:

# Inner join: only people with orders (.NET Join).
pairs = (
    flp.it(people)
    .join(orders, lambda p: p[1], lambda o: o[1], lambda p, o: (p[0], o[0]))
    .to_list()
)
assert pairs == [("Ada", 10), ("Ada", 11)]

Left join and full join

left_join keeps every outer element (.NET 10 LeftJoin). full_join also keeps the inner elements nobody matched (.NET 11 FullJoin). An unmatched side reaches the result selector as None:

# Left join: everyone, with None where there is no order (.NET 10 LeftJoin).
pairs = (
    flp.it(people)
    .left_join(orders, lambda p: p[1], lambda o: o[1], lambda p, o: (p[0], o and o[0]))
    .to_list()
)
assert pairs == [("Ada", 10), ("Ada", 11), ("Tim", None), ("Grace", None)]
# Full join: unmatched orders are appended at the end (.NET 11 FullJoin).
pairs = (
    flp.it(people)
    .full_join(orders, lambda p: p[1], lambda o: o[1], lambda p, o: (p and p[0], o and o[0]))
    .to_list()
)
assert pairs[-1] == (None, 12)

Group join

group_join yields one result per outer element, however many inner elements match (.NET GroupJoin). The result selector receives the matches as a query:

# Group join: one result per person, with all of their orders (.NET GroupJoin).
grouped = (
    flp.it(people)
    .group_join(orders, lambda p: p[1], lambda o: o[1],
                lambda p, os: (p[0], os.select(lambda o: o[0]).to_list()))
    .to_list()
)
assert grouped == [("Ada", [10, 11]), ("Tim", []), ("Grace", [])]

Cross join

cross_join has no .NET equivalent. It yields every combination, with no keys involved:

# Cross join: every combination. There is no .NET equivalent.
assert flp.it([1, 2]).cross_join(["a", "b"], lambda n, c: f"{n}{c}").to_list() == ["1a", "1b", "2a", "2b"]

Without a result selector, join, left_join, full_join and cross_join yield JoinTuple(outer, inner).

Lookups

to_lookup groups a sequence by key once and lets you query the groups by key afterwards (.NET ToLookup):

by_length = flp.it(["pear", "fig", "kiwi"]).to_lookup(len)   # enumerates the source right away

assert len(by_length) == 2                    # number of keys
assert by_length[4].to_list() == ["pear", "kiwi"]
assert by_length[9].to_list() == []           # a missing key gives an empty sequence
assert 3 in by_length

Rules worth knowing

  • Elements whose key is None never match. left_join and full_join still emit them as unmatched, and group_join emits them with an empty group.
  • full_join returns the left_join rows in outer order, followed by the unmatched inner elements in inner order.
  • Argument errors are raised immediately; everything else is deferred.
  • Keys are compared with == and hash, like group_by. Custom comparers are not supported yet.

How much each join buffers is listed in Execution characteristics.