---
title: Foundation
description: The `L` class and helpers for it
---
<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

## Foundational Functions

[`add_docs`](foundation.md#add_docs) attaches docstrings to a class and its methods from a dictionary. [`docs`](foundation.md#docs) is its decorator form. Both check that every public method has documentation.

[`coll_repr`](foundation.md#coll_repr) shows a collection's length and a preview of its items. `flatmap(f, xs)` applies `f` to each item and concatenates the results.

[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/foundation.py#L45)

### working\_directory

```python
def working_directory(
    path
):
```

*Change working directory to `path` and return to previous on exit.*

[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/foundation.py#L53)

### add\_docs

```python
def add_docs(
    cls, cls_doc=None, **docs
):
```

*Copy values from [`docs`](foundation.md#docs) to `cls` docstrings, and confirm all public methods are documented*

[`add_docs`](foundation.md#add_docs) groups a class's docstrings separately from its method definitions. This lets you document one-line methods without expanding their definitions. We discuss this style in [our style guide](https://docs.fast.ai/dev/style.html).

Suppose you have the following undocumented class:

```python
class T:
    def foo(self): pass
    def bar(self): pass
```

You can add documentation to this class like so:

```python
add_docs(T, cls_doc="A docstring for the class.",
            foo="The foo method.",
            bar="The bar method.")
```

Now, docstrings will appear as expected:

```python
test_eq(T.__doc__, "A docstring for the class.")
test_eq(T.foo.__doc__, "The foo method.")
test_eq(T.bar.__doc__, "The bar method.")
```

[`add_docs`](foundation.md#add_docs) also validates that all of  your public methods contain a docstring.  If one of your methods is not documented, it will raise an error:

```python
class T:
    def foo(self): pass
    def bar(self): pass

with expect_fail(Exception, "Missing docs"): add_docs(T, "A docstring for the class.", foo="The foo method.")
```

[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/foundation.py#L67)

### docs

```python
def docs(
    cls
):
```

*Decorator version of [`add_docs`](foundation.md#add_docs), using `_docs` dict*

To use the [`docs`](foundation.md#docs) decorator, put the docstrings in the class's `_docs` dictionary. Use method names as keys and `cls_doc` for the class docstring:

```python
@docs
class _T:
    def f(self): pass
    def g(cls): pass
    
    _docs = dict(cls_doc="The class docstring", 
                 f="The docstring for method f.",
                 g="A different docstring for method g.")

    
test_eq(_T.__doc__, "The class docstring")
test_eq(_T.f.__doc__, "The docstring for method f.")
test_eq(_T.g.__doc__, "A different docstring for method g.")
```

For either the [`docs`](foundation.md#docs) decorator or the [`add_docs`](foundation.md#add_docs) function, you can still define your docstrings in the normal way.  Below we set the docstring for the class as usual, but define the method docstrings through the `_docs` attribute:

```python
@docs
class _T:
    "The class docstring"
    def f(self): pass
    _docs = dict(f="The docstring for method f.")

    
test_eq(_T.__doc__, "The class docstring")
test_eq(_T.f.__doc__, "The docstring for method f.")
```

### is\_iter

```python
def is_iter(
    o
):
```

*Test whether `o` can be used in a `for` loop*

```python
assert is_iter([1])
assert not is_iter(array(1))
assert is_iter(array([1,2]))
assert (o for o in range(3))
```

[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/foundation.py#L73)

### coll\_repr

```python
def coll_repr(
    c, max_n:int=250
):
```

*String repr of up to `max_n` items of (possibly lazy) collection `c`*

[`coll_repr`](foundation.md#coll_repr) is used to provide a more informative [`__repr__`](https://stackoverflow.com/questions/1984162/purpose-of-pythons-repr) about list-like objects.  [`coll_repr`](foundation.md#coll_repr) and is used by [`L`](foundation.md#l) to build a `__repr__` that displays the length of a list in addition to a preview of a list.

```python
test_eq(coll_repr(range(1000),10), '(#1000) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9...]')
test_eq(coll_repr(range(1000), 5), '(#1000) [0, 1, 2, 3, 4...]')
test_eq(coll_repr(range(10),   5), '(#10) [0, 1, 2, 3, 4...]')
test_eq(coll_repr(range(5),    5), '[0, 1, 2, 3, 4]')
```

[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/foundation.py#L82)

### is\_bool

```python
def is_bool(
    x
):
```

*Check whether `x` is a bool or None*

[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/foundation.py#L87)

### mask2idxs

```python
def mask2idxs(
    mask
):
```

*Convert bool mask or index list to index [`L`](foundation.md#l)*

```python
test_eq(mask2idxs([False,True,False,True]), [1,3])
test_eq(mask2idxs(array([False,True,False,True])), [1,3])
test_eq(mask2idxs(array([1,2,3])), [1,2,3])
```

[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/basics.py#L778)

### cycle

```python
def cycle(
    o
):
```

*Like `itertools.cycle` except creates list of `None`s if `o` is empty*

```python
test_eq(itertools.islice(cycle([1,2,3]),5), [1,2,3,1,2])
test_eq(itertools.islice(cycle([]),3), [None]*3)
test_eq(itertools.islice(cycle(None),3), [None]*3)
test_eq(itertools.islice(cycle(1),3), [1,1,1])
```

[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/basics.py#L784)

### zip\_cycle

```python
def zip_cycle(
    x, *args
):
```

*Like `itertools.zip_longest` but [`cycle`](basics.md#cycle)s through elements of all but first argument*

```python
test_eq(zip_cycle([1,2,3,4],list('abc')), [(1, 'a'), (2, 'b'), (3, 'c'), (4, 'a')])
```

[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/foundation.py#L109)

### is\_indexer

```python
def is_indexer(
    idx
):
```

*Test whether `idx` will index a single item in a list*

You can, for example index a single item in a list with an integer or a 0-dimensional numpy array:

```python
assert is_indexer(1)
assert is_indexer(np.array(1))
```

However, you cannot index into single item in a list with another list or a numpy array with ndim \> 0.

```python
assert not is_indexer([1, 2])
assert not is_indexer(np.array([[1, 2], [3, 4]]))
```

[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/foundation.py#L114)

### product

```python
def product(
    xs
):
```

*The product of elements of `xs`, with `None`s removed*

```python
product([None, 3, 4, 5])
```

```output
60
```

```python
product([])
```

```output
1
```

```python
sum([])
```

```output
0
```

### flatmap

[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/foundation.py#L119)

### flatmap

```python
def flatmap(
    f, xs, **kwargs
):
```

*Apply f to each element and flatten the results into a single list.*

`flatmap(f, xs)` returns `[y for x in xs for y in f(x)]`. Each call to `f` returns an iterable. Its items become part of the resulting list:

```python
flatmap(range, range(4))
```

```output
[0, 0, 1, 0, 1, 2]
```

Compare `map` (which nests results) with [`flatmap`](foundation.md#flatmap) (which flattens them):

```python
list(map(str.split, ["hello world", "foo bar"]))  # nested
```

```output
[['hello', 'world'], ['foo', 'bar']]
```

```python
flatmap(str.split, ["hello world", "flatmap rocks"])
```

```output
['hello', 'world', 'flatmap', 'rocks']
```

Parse CSV-like lines into all values:

```python
flatmap(~Self.split(','), ["a,b,c", "d,e"])
```

```output
['a', 'b', 'c', 'd', 'e']
```

Return `[]` to skip an item, `[x]` to keep it, or `[x, y, ...]` to expand it:

```python
flatmap(lambda x: [x*10] if x else [], [1, 0, 2])  # skips zeros
```

```output
[10, 20]
```

```python
dat = [{'emails': ['a@x.com','b@x.com']}, {'emails': []}, {'emails': ['c@x.com']}]
flatmap(~Self['emails'], dat)
```

```output
['a@x.com', 'b@x.com', 'c@x.com']
```

All files in multiple directories:

```python
flatmap(~Self.iterdir(), [Path('files'), Path('images')])
```

```output
[Path('files/test.txt.bz2'),
 Path('images/mnist3.png'),
 Path('images/att_00000.png'),
 Path('images/att_00005.png'),
 Path('images/att_00007.png'),
 Path('images/att_00006.png'),
 Path('images/puppy.jpg')]
```

Pair each item with its factors:

```python
def factpairs(n): return [(n,i) for i in range(1,n+1) if n%i==0]
flatmap(factpairs, [6,10])
```

```output
[(6, 1), (6, 2), (6, 3), (6, 6), (10, 1), (10, 2), (10, 5), (10, 10)]
```

You can also use kwargs, for instance to apply `str.split` with a custom separator:

```python
flatmap(str.split, ["a-b-c", "d-e"], sep="-")
```

```output
['a', 'b', 'c', 'd', 'e']
```

## [`L`](foundation.md#l) helpers

[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/foundation.py#L124)

### CollBase

```python
def CollBase(
    items
):
```

*Base class for composing a list of `items`*

`ColBase` is a base class that emulates the functionality of a python `list`:

```python
class _T(CollBase): pass
l = _T([1,2,3,4,5])

test_eq(len(l), 5) # __len__
test_eq(l[-1], 5); test_eq(l[0], 1) #__getitem__
l[2] = 100; test_eq(l[2], 100)      # __set_item__
del l[0]; test_eq(len(l), 4)        # __delitem__
test_eq(str(l), '[2, 100, 4, 5]')   # __repr__
```

[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/foundation.py#L141)

### L

```python
def L(
    items=None, # Initial item, iterable, or `None` (empty `L`); strings, bytes, dicts, arrays, and tensors stay single items unless `use_list=True`
    *rest, # Additional positional items, e.g. `L(1, 2, 3)`
    use_list:bool=False, # `True` forces `list(items)`; `False` wraps scalars/strings/dicts as 1 item; `None` keeps array/DataFrame in place
    match=None, # Replicate a 1-item `L` to `len(match)`, or assert lengths match
):
```

*Behaves like a list of `items` but can also index with list of indices or masks*

[`L`](foundation.md#l) extends Python's list operations with indexing inspired by [NumPy](http://www.numpy.org/). You can index with an integer, slice, collection of integers or boolean mask. Methods such as `map`, `filter`, `sorted`, `unique`, `itemgot` and `attrgot` return an [`L`](foundation.md#l):

`L(None)` is empty; `L('abc')` (and bytes, dicts, arrays, tensors) stays a single item unless `use_list=True`; `use_list=None` wraps an array or DataFrame in place without copying; `L(existing_L)` returns the same object via [`_L_Meta`](foundation.md#_l_meta). Override `_new(self, items, *args, **kwargs)` in [`L`](foundation.md#l) subclasses to preserve metadata across slices and chained methods.

### Examples and overview

```python
from fastcore.utils import gt
```

Read [this overview section](https://fastcore.fast.ai/tour.html#L) for a quick tutorial of [`L`](foundation.md#l), as well as background on the name.

You can create an [`L`](foundation.md#l) from an existing iterable (e.g. a list, range, etc) and access or modify it with an int list/tuple index, mask, int, or slice. All `list` methods can also be used with [`L`](foundation.md#l).

```python
t = L(range(12))
test_eq(t, list(range(12)))
test_ne(t, list(range(11)))
t[3] = "h"
test_eq(t[3], "h")
t[3,5] = ("j","k")
test_eq(t[3,5], ["j","k"])
test_eq(t, L(t))
test_eq(L(L(1,2),[3,4]), ([1,2],[3,4]))
t[0:3] = [1, 2, 3]
test_eq(t[0:3], [1, 2, 3])
t
```

```output
[1, 2, 3, 'j', 4, 'k', 6, 7, 8, 9, 10, 11]
```

Any [`L`](foundation.md#l) is a `Sequence` so you can use it with methods like `random.sample`:

```python
assert isinstance(t, Sequence)
```

```python
import random
```

```python
random.seed(0)
random.sample(t, 3)
```

```output
[6, 11, 1]
```

There are optimized indexers for arrays, tensors, and DataFrames.

```python
import pandas as pd
```

```python
arr = np.arange(9).reshape(3,3)
t = L(arr, use_list=None)
test_eq(t[1,2], arr[[1,2]])

df = pd.DataFrame({'a':[1,2,3]})
t = L(df, use_list=None)
test_eq(t[1,2], L(pd.DataFrame({'a':[2,3]}, index=[1,2]), use_list=None))
```

You can also modify an [`L`](foundation.md#l) with `append`, `+`, and `*`.

```python
t = L()
test_eq(t, [])
t.append(1)
test_eq(t, [1])
t += [3,2]
test_eq(t, [1,3,2])
t = t + [4]
test_eq(t, [1,3,2,4])
t = 5 + t
test_eq(t, [5,1,3,2,4])
test_eq(L(1,2,3), [1,2,3])
test_eq(L(1,2,3), L(1,2,3))
t = L(1)*5
test_eq(~L([True,False,False]), L([False,True,True]))
```

You can construct an [`L`](foundation.md#l) from an iterable. By default, [`L`](foundation.md#l) keeps a tensor or array as one item. Pass `use_list=True` to collect its elements instead:

```python
test_eq(L([1,2,3]),[1,2,3])
test_eq(L(L([1,2,3])),[1,2,3])
test_ne(L([1,2,3]),[1,2,])
test_eq(L('abc'),['abc'])
test_eq(L(range(0,3)),[0,1,2])
test_eq(L(o for o in range(0,3)),[0,1,2])
test_eq(L(array(0)),[array(0)])
test_eq(L([array(0),array(1)]),[array(0),array(1)])
test_eq(L(array([0.,1.1]))[0],array([0.,1.1]))
test_eq(L(array([0.,1.1]), use_list=True), [array(0.),array(1.1)])  # `use_list=True` to unwrap arrays/arrays
```

If `match` is not `None` then the created list is same len as `match`, either by:

- If `len(items)==1` then `items` is replicated,
- Otherwise an error is raised if `match` and `items` are not already the same size.

```python
test_eq(L(1,match=[1,2,3]),[1,1,1])
test_eq(L([1,2],match=[2,3]),[1,2])
with expect_fail(): L([1,2],match=[1,2,3])
```

If you create an [`L`](foundation.md#l) from an existing [`L`](foundation.md#l) then you'll get back the original object (since [`L`](foundation.md#l) uses the [`NewChkMeta`](meta.md#newchkmeta) metaclass).

```python
test_is(L(t), t)
```

An [`L`](foundation.md#l) is considred equal to a list if they have the same elements. It's never considered equal to a `str` a `set` or a `dict` even if they have the same elements/keys.

```python
test_eq(L(['a', 'b']), ['a', 'b'])
test_ne(L(['a', 'b']), 'ab')
test_ne(L(['a', 'b']), {'a':1, 'b':2})
```

[`xdumps`](basics.md#xdumps) serializes an [`L`](foundation.md#l) as a regular list, including [`L`](foundation.md#l)s nested inside other objects. [`L`](foundation.md#l) supplies this conversion through its `__json__` method.

```python
items = dict(names=L('Alyssa', 'Ben'), scores=L(8, 9))
test_eq(json.loads(xdumps(items)), dict(names=['Alyssa','Ben'], scores=[8,9]))
```

### [`L`](foundation.md#l) Methods

[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/foundation.py#L157)

### L.\_\_getitem\_\_

```python
def __getitem__(
    idx
):
```

*Retrieve `idx` (can be list of indices, or mask, or int) items*

```python
t = L(range(12))
test_eq(t[1,2], [1,2])                # implicit tuple
test_eq(t[[1,2]], [1,2])              # list
test_eq(t[:3], [0,1,2])               # slice
test_eq(t[[False]*11 + [True]], [11]) # mask
test_eq(t[array(3)], 3)
```

Slicing or selecting from an [`L`](foundation.md#l) subclass returns the same subclass:

```python
class _Rows(L):
    def __repr__(self): return '\n'.join(f'row {o}' for o in self)

r = _Rows(range(5))
for sel in (slice(1,3), [0,2], [True,False,True,False,True], (1,2)): test_eq(type(r[sel]), _Rows)
test_eq(r[1], 1)  # a single item is still the item, not a collection
r[1:3]
```

```output
row 1
row 2
```

Override `_new` to preserve subclass attributes in results such as slices and concatenations. It receives only the resulting items. Here `_Titled` copies its title when slicing or adding items:

```python
class _Titled(L):
    def __init__(self, items=None, title=None, **kw):
        super().__init__(items, **kw)
        self.title = title
    def _new(self, items, *args, **kw):
        res = super()._new(items, *args, **kw)
        res.title = self.title
        return res

tl = _Titled(range(5), title='counts')
test_eq(tl[1:3].title, 'counts')
test_eq((tl + [5]).title, 'counts')                # one hook covers every derived collection
test_eq(_Titled(range(3))[:2].title, None)
```

Keep per-item state on the items themselves. `_new` doesn't receive the selected indices and cannot select corresponding entries from a separate attribute.

`self.items` stores the collection's contents. Use another name for subclass attributes.

`_new(items)` must preserve the items it receives. Subclasses must also preserve indexing and iteration behavior. For a single index, `x[i]` must return `x.items[i]`. `list(x)` must equal `list(x.items)`. Methods such as `map` and `filter` depend on these rules. For a lazy view that transforms items on access, store an [`L`](foundation.md#l) as an attribute instead of subclassing it.

To select raw items into a plain [`L`](foundation.md#l), use `L(x.items, use_list=None)[idx]`. `use_list=None` preserves an array-backed collection. Calling `super().__getitem__` can still invoke overridden methods.

[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/foundation.py#L169)

### L.\_\_setitem\_\_

```python
def __setitem__(
    idx, o
):
```

*Set `idx` (can be list of indices, or mask, or int) items to `o` (which is broadcast if not iterable)*

```python
t[4,6] = 0
test_eq(t[4,6], [0,0])
t[4,6] = [1,2]
test_eq(t[4,6], [1,2])
```

[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/foundation.py#L210)

### L.unique

```python
def unique(
    sort:bool=False, bidir:bool=False, start=None
):
```

*Unique items, in stable order*

```python
test_eq(L(4,1,2,3,4,4).unique(), [4,1,2,3])
```

[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/foundation.py#L216)

### L.val2idx

```python
def val2idx():
```

*Dict from value to index*

```python
test_eq(L(1,2,3).val2idx(), {3:2,1:0,2:1})
```

[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/foundation.py#L222)

### L.range

```python
def range(
    a, b=None, step=None
):
```

*Class Method: Same as `range`, but returns [`L`](foundation.md#l). Can pass collection for `a`, to use `len(a)`*

```python
test_eq_type(L.range([1,1,1]), L(range(3)))
test_eq_type(L.range(5,2,2), L(range(5,2,2)))
```

[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/foundation.py#L228)

### L.enumerate

```python
def enumerate():
```

*Same as `enumerate`*

```python
test_eq(L('a','b','c').enumerate(), [(0,'a'),(1,'b'),(2,'c')])
```

[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/foundation.py#L234)

### L.renumerate

```python
def renumerate():
```

*Same as [`renumerate`](basics.md#renumerate)*

```python
test_eq(L('a','b','c').renumerate(), [('a', 0), ('b', 1), ('c', 2)])
```

[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/foundation.py#L240)

### L.split

```python
def split(
    s, sep=None, maxsplit:int=-1
):
```

*Class Method: Same as `str.split`, but returns an [`L`](foundation.md#l)*

[`L.split`](foundation.md#l.split) is a class method that works like `str.split`, but returns an [`L`](foundation.md#l) instead of a list:

```python
test_eq(L.split('a b c'), ['a','b','c'])
test_eq(L.split('a-b-c', '-'), ['a','b','c'])
test_eq(L.split('a-b-c', '-', maxsplit=1), ['a','b-c'])
```

[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/foundation.py#L246)

### L.splitlines

```python
def splitlines(
    s, keepends:bool=False
):
```

*Class Method: Same as `str.splitlines`, but returns an [`L`](foundation.md#l)*

[`L.splitlines`](foundation.md#l.splitlines) is a class method that works like `str.splitlines`, but returns an [`L`](foundation.md#l) instead of a list:

```python
test_eq(L.splitlines('a\nb\nc'), ['a','b','c'])
test_eq(L.splitlines('a\nb\nc', keepends=True), ['a\n','b\n','c'])
```

[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/foundation.py#L251)

### curryable

```python
def curryable(
    f
):
```

Call a [`curryable`](foundation.md#curryable) method on the class to create a function that accepts an iterable later. For example, you can convert each nested list with a lambda:

```python
L(lines).map(lambda x: L(x).map(int))
```

Or pass the curried `L.map(int)`:

```python
L(lines).map(L.map(int))
```

Curried calls also work with adapters such as [`star`](foundation.md#star). To multiply the pairs inside each sublist:

```python
nested.map(L.map(star(operator.mul)))
```

### map

```python
def map(
    f, *args, **kwargs
):
```

*Create new [`L`](foundation.md#l) with `f` applied to all `items`, passing `args` and `kwargs` to `f`*

```python
t = L(1,2,3)
t.append(4)
test_eq(t[[0,3]], [1,4])
test_eq(t[[False,True,False,True]], [2,4])
test_eq(t.map(lambda o:o*2), [2,4,6,8])
```

Call `L.map(int)` on the class to get a function that converts an iterable's items to integers. This curried form works with `map`, `filter`, [`groupby`](basics.md#groupby), [`argwhere`](basics.md#argwhere), `argfirst`, [`first`](basics.md#first), [`last`](basics.md#last), `sorted`, `reduce`, [`partition`](basics.md#partition), `takewhile`, `dropwhile` and `accumulate`.

`star(f)` unpacks the last argument before calling `f`. In `map`, that argument is the item. In `reduce`, it is the item after the accumulator. [`rstar`](foundation.md#rstar) reverses the unpacked arguments.

```python
test_eq(L.range(4).map(operator.neg), [0,-1,-2,-3])
```

If `f` is a string then it is treated as a format string to create the mapping:

```python
test_eq(L.range(4).map('#{}#'), ['#0#','#1#','#2#','#3#'])
```

For a dictionary or other indexable object `f`, `map` looks up `f[item]` for each item:

```python
test_eq(L.range(4).map(list('abcd')), list('abcd'))
```

You can also pass the same `arg` params that [`bind`](basics.md#bind) accepts:

```python
def f(a=None,b=None): return b
test_eq(L.range(4).map(f, b=arg0), range(4))
```

[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/foundation.py#L266)

### star

```python
def star(
    f
):
```

*Adapt `f` to unpack its last argument, e.g. for use in `map`-style functions*

`star(f)` unpacks the last argument before calling `f`. Use it when each item contains multiple arguments. With `reduce`, the accumulator stays first and the item's values follow it:

```python
test_eq(L((1,2),(3,4)).map(star(operator.add)), [3,7])
```

[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/foundation.py#L273)

### rstar

```python
def rstar(
    f
):
```

*Like [`star`](foundation.md#star), but unpack the last argument in reverse order*

[`rstar`](foundation.md#rstar) is the same adapter with the unpacked arguments reversed, for when the function expects them in the opposite order to how they're stored:

```python
test_eq(L((1,2),(4,7)).map(rstar(operator.sub)), [1,3])  # b-a
```

[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/foundation.py#L280)

### splitter

```python
def splitter(
    sep=None, maxsplit:int=-1
):
```

*Create a partial function that splits strings into [`L`](foundation.md#l)*

A curried version of [`L.split`](foundation.md#l.split), useful for mapping over collections of strings. For instance to split some lines with the same separator:

```python
data = '''1,2,3
4,5,6
7,8,9'''

grid = L.splitlines(data).map(splitter(','))
grid
```

```output
[['1', '2', '3'], ['4', '5', '6'], ['7', '8', '9']]
```

Use the curried `L.map(int)` to convert every row of `grid` to integers:

```python
intgrid = grid.map(L.map(int))
intgrid
```

```output
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
```

Although in this particular example numpy has a useful shortcut:

```python
np.genfromtxt(data.splitlines(), delimiter=',', dtype=int)
```

```output
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])
```

[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/foundation.py#L285)

### linesplitter

```python
def linesplitter(
    keepends:bool=False
):
```

*Create a partial function that splits strings by lines into [`L`](foundation.md#l)*

A curried version of [`L.splitlines`](foundation.md#l.splitlines), useful for splitting multi-line strings into [`L`](foundation.md#l)s when mapping over a collection.

```python
L(['a\nb\nc', 'd\ne']).map(linesplitter())
```

```output
[['a', 'b', 'c'], ['d', 'e']]
```

[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/basics.py#L748)

### groupby

```python
def groupby(
    key, val:function=noop
):
```

*Same as [`fastcore.basics.groupby`](basics.md#groupby)*

```python
words = L.split('aaa abc bba')
test_eq(words.groupby(0, (1,2)), {'a':[('a','a'),('b','c')], 'b':[('b','a')]})
```

Group each inner collection's strings by their first character:

```python
L([['a1','b2','a3'], ['x1','y2','x3']]).map(L.groupby(0))
```

```output
[{'a': ['a1', 'a3'], 'b': ['b2']}, {'x': ['x1', 'x3'], 'y': ['y2']}]
```

[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/foundation.py#L298)

### L.map\_dict

```python
def map_dict(
    f:function=noop, *args, **kwargs
):
```

*Like `map`, but creates a dict from `items` to function results*

```python
test_eq(L(range(1,5)).map_dict(), {1:1, 2:2, 3:3, 4:4})
test_eq(L(range(1,5)).map_dict(operator.neg), {1:-1, 2:-2, 3:-3, 4:-4})
```

[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/foundation.py#L304)

### L.zip

```python
def zip(
    cycled:bool=False
):
```

*Create new [`L`](foundation.md#l) with `zip(*items)`*

```python
t = L([[1,2,3],'abc'])
test_eq(t.zip(), [(1, 'a'),(2, 'b'),(3, 'c')])
```

```python
t = L([[1,2,3,4],['a','b','c']])
test_eq(t.zip(cycled=True ), [(1, 'a'),(2, 'b'),(3, 'c'),(4, 'a')])
test_eq(t.zip(cycled=False), [(1, 'a'),(2, 'b'),(3, 'c')])
```

[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/foundation.py#L310)

### L.map\_zip

```python
def map_zip(
    f, *args, cycled:bool=False, **kwargs
):
```

*Apply `f` to `zip` of items, unpacking each zipped tuple*

```python
t = L([1,2,3],[2,3,4])
test_eq(t.map_zip(operator.mul), [2,6,12])
```

[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/foundation.py#L316)

### L.zipwith

```python
def zipwith(
    *rest, cycled:bool=False
):
```

*Create new [`L`](foundation.md#l) with `self` zip with each of `*rest`*

```python
b = [[0],[1],[2,2]]
t = L([1,2,3]).zipwith(b)
test_eq(t, [(1,[0]), (2,[1]), (3,[2,2])])
```

[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/foundation.py#L322)

### L.map\_zipwith

```python
def map_zipwith(
    f, *rest, cycled:bool=False, **kwargs
):
```

*Apply `f` to `zipwith` of items, unpacking each zipped tuple*

```python
test_eq(L(1,2,3).map_zipwith(operator.mul, [2,3,4]), [2,6,12])
```

### filter

```python
def filter(
    f:function=noop, negate:bool=False, **kwargs
):
```

*Create new [`L`](foundation.md#l) filtered by predicate `f`, passing `args` and `kwargs` to `f`*

```python
t = L(range(12))
test_eq(t.filter(lambda o:o<5), [0,1,2,3,4])
test_eq(t.filter(lambda o:o<5, negate=True), [5,6,7,8,9,10,11])
```

Filter each row of `intgrid` with a curried call:

```python
intgrid
```

```output
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
```

```python
intgrid.map(L.filter(ge(5)))
```

```output
[[], [5, 6], [7, 8, 9]]
```

[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/basics.py#L807)

### argwhere

```python
def argwhere(
    f, negate:bool=False, **kwargs
):
```

*Like `filter`, but return indices for matching items*

```python
t = L([0,1,2,3,4,99,0])
test_eq(t.argwhere(lambda o:o<5), [0,1,2,3,4,6])
```

[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/basics.py#L835)

### first

```python
def first(
    f, negate:bool=False
):
```

*Return first matching item*

```python
test_eq(t.first(lambda o:o>4), 99)
test_eq(t.first(lambda o:o>4,negate=True), 0)
```

```python
nested = L([[1,2,8,4], [5,9,7], [1,1,1]])
nested.map(L.first(gt(5)))
```

```output
[8, 9, None]
```

[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/basics.py#L842)

### last

```python
def last(
    f, negate:bool=False
):
```

*Return last matching item*

```python
test_eq(L(5,4,3,2,99,1).last(lambda o:o>4), 99)
test_eq(L(5,4,3,2,99,1).last(lambda o:o>4,negate=True), 1)
```

```python
nested = L([[1,2,8,4], [5,9,7], [1,1,1]])
nested.map(L.last(gt(5)))
```

```output
[8, 7, None]
```

### argfirst

```python
def argfirst(
    f, negate:bool=False
):
```

*Return index of first matching item*

```python
test_eq(t.argfirst(lambda o:o>4), 5)
test_eq(t.argfirst(lambda o:o>4,negate=True),0)
```

Find the index of the first value greater than 5 in each inner collection:

```python
nested = L([[1,2,8,4], [5,9,7], [1,1,1]])
nested.map(L.argfirst(gt(5)))
```

```output
[2, 1, None]
```

[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/foundation.py#L364)

### L.itemgot

```python
def itemgot(
    *idxs
):
```

*Create new [`L`](foundation.md#l) with item `idx` of all `items`*

```python
t = L([['x', [0]], ['y', [1]], ['z', [2,2]]])
test_eq(t.itemgot(1), b)
```

[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/foundation.py#L372)

### L.attrgot

```python
def attrgot(
    k, default=None
):
```

*Create new [`L`](foundation.md#l) with attr `k` (or value `k` for dicts) of all `items`.*

```python
# Example when items are not a dict
a = [SimpleNamespace(a=3,b=4),SimpleNamespace(a=1,b=2)]
test_eq(L(a).attrgot('b'), [4,2])

#Example of when items are a dict
b =[{'id': 15, 'name': 'nbdev'}, {'id': 17, 'name': 'fastcore'}]
test_eq(L(b).attrgot('id'), [15, 17])
```

### sorted

```python
def sorted(
    key=None, reverse:bool=False, cmp=None, **kwargs
):
```

*New [`L`](foundation.md#l) sorted by `key`, using `sort_ex`. If key is str use `attrgetter`; if int use `itemgetter`*

```python
test_eq(L(a).sorted('a').attrgot('b'), [2,4])
```

Sort each inner collection by the first element of each tuple:

```python
nested = L([[(3,'c'),(1,'a'),(2,'b')], [(6,'f'),(4,'d')]])
nested.map(L.sorted(0))
```

```output
[[(1, 'a'), (2, 'b'), (3, 'c')], [(4, 'd'), (6, 'f')]]
```

[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/foundation.py#L385)

### L.concat

```python
def concat():
```

*Concatenate all elements of list*

```python
test_eq(L([0,1,2,3],4,L(5,6)).concat(), range(7))
```

[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/foundation.py#L391)

### L.copy

```python
def copy():
```

*Same as `list.copy`, but returns an [`L`](foundation.md#l)*

[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/foundation.py#L397)

### L.\_\_copy\_\_

```python
def __copy__():
```

`copy.copy` creates a separate items list. Appending to the copy leaves the original unchanged:

```python
a = L(1,2,3)
b = copy(a)
b.append(4)
test_eq(a, [1,2,3])
test_eq(b, [1,2,3,4])
```

```python
t = L([0,1,2,3],4,L(5,6)).copy()
test_eq(t.concat(), range(7))
```

[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/foundation.py#L401)

### L.shuffle

```python
def shuffle():
```

*Same as `random.shuffle`, but not inplace*

[`L.shuffle`](foundation.md#l.shuffle) returns a new shuffled [`L`](foundation.md#l), leaving the original unchanged:

```python
t = L(1,2,3,4,5)
s = t.shuffle()
test_eq(set(s), set(t))  # same elements
test_eq(t, [1,2,3,4,5])  # original unchanged
```

### reduce

```python
def reduce(
    f, initial=None
):
```

*Wrapper for `functools.reduce`*

```python
test_eq(L(1,2,3,4).reduce(operator.add), 10)
test_eq(L(1,2,3,4).reduce(operator.mul, 10), 240)
```

Sum each inner collection:

```python
nested = L([[1,2,3], [4,5], [6,7,8,9]])
nested.map(L.reduce(operator.add))
```

```output
[6, 9, 30]
```

E.g implement a dot product:

```python
def dot(a,b): return a.zipwith(b).reduce(star(lambda acc,a,b: acc+a*b), 0)
dot(L(1,3,5), L(2,4,6))
```

```output
44
```

[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/foundation.py#L416)

### L.sum

```python
def sum():
```

*Sum of the items*

```python
test_eq(L(1,2,3,4).sum(), 10)
test_eq(L().sum(), 0)
```

[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/foundation.py#L422)

### L.product

```python
def product():
```

*Product of the items*

```python
test_eq(L(1,2,3,4).product(), 24)
test_eq(L().product(), 1)
```

[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/foundation.py#L428)

### L.map\_first

```python
def map_first(
    f:function=noop, g:function=noop, *args, **kwargs
):
```

*First element of `map_filter`*

```python
t = L(0,1,2,3)
test_eq(t.map_first(lambda o:o*2 if o>2 else None), 6)
```

[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/foundation.py#L434)

### L.setattrs

```python
def setattrs(
    attr, val
):
```

*Call `setattr` on all items*

```python
t = L(SimpleNamespace(),SimpleNamespace())
t.setattrs('foo', 'bar')
test_eq(t.attrgot('foo'), ['bar','bar'])
```

[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/foundation.py#L440)

### L.flatmap

```python
def flatmap(
    f, **kwargs
):
```

*Apply f to each element and flatten the results into a single L.*

[`L.flatmap`](foundation.md#l.flatmap) applies [`flatmap`](foundation.md#flatmap) and returns an [`L`](foundation.md#l):

```python
test_eq(L("a,b,c", "d,e").flatmap(~Self.split(',')), ['a', 'b', 'c', 'd', 'e'])
```

or alternatively use kwargs:

```python
test_eq(L("a-b-c", "d-e").flatmap(str.split, sep='-'), ['a', 'b', 'c', 'd', 'e'])
```

As an alternative, you can just chain `map` and [`concat`](basics.md#concat):

```python
L("a,b,c", "d,e").map(~Self.split(',')).concat()
```

```output
['a', 'b', 'c', 'd', 'e']
```

```python
L("a-b-c", "d-e").map(str.split, sep='-').concat()
```

```output
['a', 'b', 'c', 'd', 'e']
```

### itertools wrappers

[`L`](foundation.md#l) has methods for `itertools` operations including [`cycle`](basics.md#cycle), `takewhile`, `dropwhile`, `accumulate`, `pairwise`, `batched`, `compress`, `permutations` and `combinations`.

[`partition`](basics.md#partition) returns two collections according to a predicate. [`flatten`](basics.md#flatten) recursively flattens nested iterables, leaving strings intact.

[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/foundation.py#L446)

### L.cycle

```python
def cycle():
```

*Same as `itertools.cycle`*

[`L.cycle`](foundation.md#l.cycle) returns an infinite iterator that cycles through the elements:

```python
test_eq(list(itertools.islice(L(1,2,3).cycle(), 7)), [1,2,3,1,2,3,1])
```

### takewhile

```python
def takewhile(
    f
):
```

*Same as `itertools.takewhile`*

[`L.takewhile`](foundation.md#l.takewhile) returns elements from the beginning of the list while the predicate is true:

```python
test_eq(L(1,2,3,4,5,1,2).takewhile(lambda x: x<4), [1,2,3])
test_eq(L(1,2,3,11).takewhile(lt(10)), [1,2,3])
```

Take values from each inner collection until reaching one that is 5 or greater:

```python
nested = L([[1,2,5,3], [2,3,8,1], [9,1,2]])
nested.map(L.takewhile(lt(5)))
```

```output
[[1, 2], [2, 3], []]
```

### dropwhile

```python
def dropwhile(
    f
):
```

*Same as `itertools.dropwhile`*

[`L.dropwhile`](foundation.md#l.dropwhile) skips elements from the beginning while the predicate is true, then returns the rest:

```python
test_eq(L(1,2,3,4,5,1,2).dropwhile(lt(4)), [4,5,1,2])
test_eq(L(1,2,3).dropwhile(lt(10)), [])
```

### accumulate

```python
def accumulate(
    f:builtin_function_or_method=add, initial=None
):
```

*Same as `itertools.accumulate`*

[`L.accumulate`](foundation.md#l.accumulate) returns running totals (or running results of any binary function):

```python
test_eq(L(1,2,3,4).accumulate(), [1,3,6,10])
test_eq(L(1,2,3,4).accumulate(operator.mul), [1,2,6,24])
test_eq(L(1,2,3).accumulate(initial=10), [10,11,13,16])
```

Calculate running products for each inner collection:

```python
nested = L([[1,2,3], [4,5,6], [10,20]])
nested.map(L.accumulate(operator.mul))
```

```output
[[1, 2, 6], [4, 20, 120], [10, 200]]
```

[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/foundation.py#L473)

### L.pairwise

```python
def pairwise():
```

*Same as `itertools.pairwise`*

[`L.pairwise`](foundation.md#l.pairwise) returns consecutive overlapping pairs:

```python
test_eq(L(1,2,3,4).pairwise(), [(1,2),(2,3),(3,4)])
test_eq(L(list('abcd')).pairwise(), [('a','b'),('b','c'),('c','d')])
```

[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/foundation.py#L491)

### L.batched

```python
def batched(
    n
):
```

*Same as `itertools.batched` (but also works on older Python versions*

[`L.batched`](foundation.md#l.batched) splits into chunks of size `n`:

```python
test_eq(L(1,2,3,4,5).batched(2), [(1,2),(3,4),(5,)])
test_eq(L(list('abcdefg')).batched(3), [('a','b','c'),('d','e','f'),('g',)])
```

[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/foundation.py#L497)

### L.compress

```python
def compress(
    selectors
):
```

*Same as `itertools.compress`*

[`L.compress`](foundation.md#l.compress) filters elements using a boolean selector:

```python
test_eq(L(list('abcd')).compress([1,0,1,0]), ['a','c'])
test_eq(L(1,2,3,4,5).compress([True,False,True,False,True]), [1,3,5])
```

[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/foundation.py#L503)

### L.permutations

```python
def permutations(
    r=None
):
```

*Same as `itertools.permutations`*

[`L.permutations`](foundation.md#l.permutations) returns all permutations of length `r` (defaults to full length):

```python
test_eq(L(1,2,3).permutations(), [(1,2,3),(1,3,2),(2,1,3),(2,3,1),(3,1,2),(3,2,1)])
test_eq(L(list('abc')).permutations(2), [('a','b'),('a','c'),('b','a'),('b','c'),('c','a'),('c','b')])
```

[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/foundation.py#L509)

### L.combinations

```python
def combinations(
    r
):
```

*Same as `itertools.combinations`*

[`L.combinations`](foundation.md#l.combinations) returns all combinations of length `r`:

```python
test_eq(L(1,2,3,4).combinations(2), [(1,2),(1,3),(1,4),(2,3),(2,4),(3,4)])
test_eq(L(list('abcd')).combinations(3), [('a','b','c'),('a','b','d'),('a','c','d'),('b','c','d')])
```

[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/basics.py#L685)

### partition

```python
def partition(
    f:function=noop, **kwargs
):
```

*Split into two [`L`](foundation.md#l)s based on predicate `f`: (true\_items, false\_items)*

[`L.partition`](foundation.md#l.partition) returns two [`L`](foundation.md#l)s: items for which `f` is true, then items for which it is false:

```python
t,f = L(1,2,3,4,5,6).partition(lambda x: x%2==0)
test_eq(t, [2,4,6])
test_eq(f, [1,3,5])

evens,odds = L.range(10).partition(lambda x: x%2==0)
test_eq(evens, [0,2,4,6,8])
test_eq(odds, [1,3,5,7,9])
```

Split each inner collection into values greater than 5 and the rest:

```python
nested = L([[1,2,3,4,5], [10,15,20,25], [3,6,9]])
nested.map(L.partition(gt(5)))
```

```output
[([], [1, 2, 3, 4, 5]), ([10, 15, 20, 25], []), ([6, 9], [3])]
```

[source](https://github.com/AnswerDotAI/fastcore/blob/main/fastcore/foundation.py#L524)

### L.flatten

```python
def flatten():
```

*Recursively flatten nested iterables (except strings)*

[`L.flatten`](foundation.md#l.flatten) recursively flattens nested iterables into a single [`L`](foundation.md#l). Strings are treated as atomic (not iterated over):

```python
test_eq(L([[1,2],[3,[4,5]]]).flatten(), [1,2,3,4,5])
test_eq(L([1,[2,[3,[4]]]]).flatten(), [1,2,3,4])
test_eq(L(['a',['b','c'],'d']).flatten(), ['a','b','c','d'])  # strings not flattened
test_eq(L([1,2,3]).flatten(), [1,2,3])  # already flat
```

Since [`star`](foundation.md#star) and [`rstar`](foundation.md#rstar) are plain function adapters, they compose with all of [`L`](foundation.md#l)'s methods, including the curried forms:

```python
test_eq(L((1,2),(3,1),(2,3)).filter(star(lt)), [(1,2),(2,3)])
test_eq(L((3,1),(1,2),(2,0)).sorted(star(operator.sub)), [(1,2),(3,1),(2,0)])
test_eq(L((1,2),(3,4),(5,6)).reduce(star(lambda acc,a,b: acc+a*b), 0), 44)
test_eq(L((2,1),(3,2),(1,4)).takewhile(rstar(lt)), [(2,1),(3,2)])
test_eq(L([[(1,2),(3,4)], [(5,6)]]).map(L.map(star(operator.mul))), [[2,12],[30]])
```
