Install & Compatibility
Where this runs
tested against v2.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.050s · 18MB
glibcpy 3.10–3.95 runs
installs and imports cleanly · install 1.5s · import 0.048s · 19MB
16MB installed
● package 16MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
lmap
✓ from funcy import lmap
Prefer specific imports over `from funcy import *` to avoid namespace pollution and improve readability.
lflatten
✓ from funcy import lflatten
Many sequence-transforming functions have an `l` prefix (e.g., `lflatten`) to return a list, while the non-prefixed version (e.g., `flatten`) returns an iterator following Python 3 conventions.
merge
✓ from funcy import merge
Common utilities like `merge` for collections are available directly from the top-level `funcy` module.
This quickstart demonstrates core Funcy operations: `lflatten` for un-nesting collections, `lmap` and `lfilter` for transforming and selecting elements from sequences, `merge` for combining dictionaries, and `first` combined with `drop` and `count` for working with iterators.
from funcy import lmap, lfilter, lflatten, merge, first, drop, count
# Flatten a nested list
data = [1, 2, [3, 4], 5, [6, [7, 8]]]
flattened = lflatten(data)
print(f"Flattened: {flattened}")
# Map and filter a sequence
numbers = [1, 2, 3, 4, 5, 6]
even_squares = lmap(lambda x: x**2, lfilter(lambda x: x % 2 == 0, numbers))
print(f"Even squares: {even_squares}")
# Merge dictionaries
dict1 = {'a': 1, 'b': 2}
dict2 = {'b': 3, 'c': 4}
merged_dict = merge(dict1, dict2)
print(f"Merged dict: {merged_dict}")
# Get the Nth item from an infinite sequence using iterators
third_item = first(drop(2, count(1)))
print(f"Third item in count(1): {third_item}")
Debug
Known issues
breakingWhen migrating from Python 2 to Python 3, `funcy` versions 0.9 and above adopted Python 3's 'iterator by default' convention for functions like `map` and `filter`. This means functions that previously returned lists might now return iterators, potentially breaking code expecting list results.fixFor code expecting lists, explicitly use the `l`-prefixed versions of functions (e.g., `lmap`, `lfilter`, `lkeep`) or convert iterator results to lists (e.g., `list(map(...))`). Refer to the documentation's 'Python 3 support' section for a full list of renamed functions.
affects: prior to 0.9 on Python 2
gotchaMany Funcy functions that operate on sequences offer two versions: one that returns an iterator (e.g., `flatten`, `chunks`) and one that returns an immediate list (e.g., `lflatten`, `lchunks`). Confusing these can lead to unexpected behavior or performance issues.fixAlways be mindful of the desired return type. Use the `l`-prefixed functions for concrete list results when immediate evaluation is needed, and the non-prefixed versions for lazy, iterator-based processing. Check the cheatsheet or function signatures for clarity.
affects: 2.0 and earlier
gotchaFuncy does not natively support method chaining for data transformations in the same way some other functional libraries or Pandas do. If you expect to call `funcy` functions as methods on data structures, it won't work out of the box.fixFor method chaining, consider using wrapper libraries like `funcy-chain` or `funcy-pipe` if that programming style is essential. Otherwise, compose functions using nested calls or assign intermediate results to variables.
affects: All versions
gotchaWhile some older examples or quick experiments might use `from funcy import *`, this practice is discouraged for production code. Funcy is a comprehensive library, and wildcard imports can easily lead to namespace collisions and make code harder to read and debug.fixAlways import specific functions you need (e.g., `from funcy import lmap, lfilter`). This improves code clarity, prevents unexpected overrides, and makes it clear which functions are being used from `funcy`.
affects: All versions
Errors
Common errors & fixes
AttributeError: 'function' object has no attribute 'some_method'
This error occurs when you attempt to call a method or access an attribute on a function object itself, rather than on the result of executing that function. In functional programming contexts, it's common to pass functions around, and sometimes a developer might forget to call the function to get its return value before trying to interact with it as a data structure.
fixEnsure that the function is called (e.g., `my_function()` instead of `my_function`) to retrieve its return value before attempting to access methods or attributes on it. For example, if `foo` is a function, `foo.strip()` will fail, but `foo().strip()` might work if `foo` returns a string.
ModuleNotFoundError: No module named 'funcy'
This error indicates that the Python interpreter cannot find the 'funcy' package. This typically happens if the library is not installed, or if it's installed in a different Python environment than the one currently being used.
fixInstall the funcy library using pip: `pip install funcy`. If already installed, ensure your Python environment is correctly activated.
ImportError: cannot import name 'some_function' from 'funcy'
This error occurs when you try to import a specific function or object from the 'funcy' library that either does not exist, is misspelled, or is not directly exposed for top-level import.
fixVerify the spelling of the function name and consult the funcy documentation (e.g., the cheatsheet or module-specific sections) to confirm the correct function name and its import path. Most `funcy` utilities are available directly from the top-level `funcy` module, so typically `from funcy import correct_function_name` should work if the name is right.
TypeError: reduce() of empty sequence with no initial value
This error is raised by `functools.reduce` (and similar reduction operations) when it's called with an empty sequence and no initial value is provided to serve as a starting point for the reduction.
fixProvide an initial value (the third argument) to the `reduce` function. This value will be returned if the sequence is empty, or used as the starting accumulator if the sequence is not empty. Example: `functools.reduce(operator.add, [], 0)` will return `0` instead of raising an error.
TypeError: 'map' object is not subscriptable
In Python 3 (and by extension, funcy's default iterator-returning functions like `map` and `filter`), `map` returns an iterator object, not a list. This error occurs when you attempt to access elements by index (e.g., `my_map_object[0]`) on an iterator, which does not support subscripting.
fixConvert the iterator to a list if you need indexed access or want to consume it multiple times. For example, `list(map(func, seq))` will create a list. Alternatively, use funcy's list-returning variants, such as `lmap()` or `lfilter()`, if you consistently need a list result: `from funcy import lmap; lmap(func, seq)`.
Upgrade
Version history
2.0latest on PyPI · released Mar 28, 2023
Audit
Dependencies
No dependency data recorded yet.