{"id":1834,"library":"funcy","title":"Funcy","description":"Funcy is a Python library offering a collection of practical functional tools, inspired by Clojure and Underscore.js. It aims to simplify data manipulation and function composition, providing utilities for working with collections, dictionaries, and functions in a functional style. The current stable version is 2.0, released in March 2023, and it supports Python 3.4+ and PyPy3.","status":"active","version":"2.0","language":"en","source_language":"en","source_url":"http://github.com/Suor/funcy","tags":["functional programming","utilities","collections","iterators","decorators","data transformation"],"install":[{"cmd":"pip install funcy","lang":"bash","label":"Install Funcy"}],"dependencies":[],"imports":[{"note":"Prefer specific imports over `from funcy import *` to avoid namespace pollution and improve readability.","symbol":"lmap","correct":"from funcy import lmap"},{"note":"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.","symbol":"lflatten","correct":"from funcy import lflatten"},{"note":"Common utilities like `merge` for collections are available directly from the top-level `funcy` module.","symbol":"merge","correct":"from funcy import merge"}],"quickstart":{"code":"from funcy import lmap, lfilter, lflatten, merge, first, drop, count\n\n# Flatten a nested list\ndata = [1, 2, [3, 4], 5, [6, [7, 8]]]\nflattened = lflatten(data)\nprint(f\"Flattened: {flattened}\")\n\n# Map and filter a sequence\nnumbers = [1, 2, 3, 4, 5, 6]\neven_squares = lmap(lambda x: x**2, lfilter(lambda x: x % 2 == 0, numbers))\nprint(f\"Even squares: {even_squares}\")\n\n# Merge dictionaries\ndict1 = {'a': 1, 'b': 2}\ndict2 = {'b': 3, 'c': 4}\nmerged_dict = merge(dict1, dict2)\nprint(f\"Merged dict: {merged_dict}\")\n\n# Get the Nth item from an infinite sequence using iterators\nthird_item = first(drop(2, count(1)))\nprint(f\"Third item in count(1): {third_item}\")","lang":"python","description":"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."},"warnings":[{"fix":"For 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.","message":"When 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.","severity":"breaking","affected_versions":"prior to 0.9 on Python 2"},{"fix":"Always 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.","message":"Many 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.","severity":"gotcha","affected_versions":"2.0 and earlier"},{"fix":"For 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.","message":"Funcy 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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always 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`.","message":"While 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.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}