Install & Compatibility
Where this runs
tested against v2.0.2 · 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.000s · 17.8MB
glibcpy 3.10–3.95 runs
installs and imports cleanly · install 1.5s · import 0.000s · 18MB
16MB installed
● package 16MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
first
✓ from first import first
Demonstrates the primary usage of the `first` function, including basic truthy checks, custom `key` functions, and specifying a `default` return value. It also includes an example of its common use with regular expressions.
from first import first
# Basic usage: returns the first truthy item
value1 = first([0, None, False, [], (), 42])
print(f"First truthy value: {value1}") # Expected: 42
# Using a key function: returns the first even number
value2 = first([1, 1, 3, 4, 5], key=lambda x: x % 2 == 0)
print(f"First even number: {value2}") # Expected: 4
# Using a default value if no truthy item is found
value3 = first([0, None, False, [], ()], default='no match')
print(f"With default: {value3}") # Expected: no match
# Useful with regular expressions
import re
re1 = re.compile('b(.*)')
re2 = re.compile('a(.*)')
m = first(regexp.match('abc') for regexp in [re1, re2])
if m:
print(f"Regex match group 1: {m.group(1)}") # Expected: bc
Debug
Known issues
breakingVersion 2.0.0 dropped explicit support for Python 3.3. The library now officially supports Python 2.7 and Python 3.4+.fixEnsure your project runs on Python 2.7 or Python 3.4+. Upgrade your Python interpreter if necessary.
affects: 2.0.0+
breakingVersion 2.0.0 removed `six` as a dependency. While unlikely to directly break user code, it reflects an internal refactoring for Python 2/3 compatibility.fixNo direct action required unless you were relying on `first` to transitively install `six`.
affects: 2.0.0+
gotchaThe `first` function's definition of 'true value' (often called 'truthy') is consistent with Python's `any()` and `all()` functions. This means values like `True`, `1`, `'foo'`, or `[None]` are considered truthy, while `None`, `False`, `[]`, `''`, or `0` are falsy. Be mindful of this behavior, especially when dealing with empty collections or zero values.fixIf different truthiness logic is required, use the `key` argument, e.g., `first(items, key=lambda x: x == 'specific_value')`.
affects: All versions
deprecatedAlthough `first` version 2.x wheels (`first-2.0.2-py2.py3-none-any.whl`) indicate Python 2 compatibility, Python 2 reached its End-of-Life (EOL) in January 2020. Running new or updated projects on Python 2 is highly discouraged due to lack of security updates and community support.fixMigrate your project to Python 3.4+ to leverage modern Python features and maintain security.
affects: All versions (contextual for Python 2 usage)
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'first'
The 'first' package is not installed in the Python environment being used, or there is a naming conflict with a local file or directory named 'first.py' or 'first' which shadows the installed library.
fixEnsure the library is installed with `pip install first`. If already installed, check for local files or directories named 'first' that might be causing an import conflict, or verify that the correct Python environment is active.
TypeError: 'int' object is not callable
This error occurs when a non-callable object (like an integer) is passed to the `key` argument of the `first()` function, which expects a callable (a function) to determine the truthiness of elements.
fixProvide a callable function (or `None`) to the `key` argument, such as a lambda function or a defined function. For example, `first([1, 2, 3], key=lambda x: x % 2 == 0)` or `first([1, 2, 3], key=None)`.
TypeError: 'str' object is not callable
This error can occur if a string is incorrectly passed as the `key` argument to the `first()` function, or if a variable named `str` (or another built-in type) has inadvertently shadowed the actual callable.
fixEnsure that the value passed to the `key` argument is a callable (a function) or `None`. If the error is due to shadowing a built-in, rename the conflicting variable. For example, `first(['a', 'b', 'c'], key=len)`.
AttributeError: module 'first' has no attribute 'first'
This happens when the `first` module is imported using `import first` and then an attempt is made to call `first()` directly, rather than accessing the function via `first.first()`. The library is designed for the function to be imported directly, e.g., `from first import first`.
fixChange the import statement to `from first import first` and then call the function as `first(iterable)`, or if keeping `import first`, call it as `first.first(iterable)`.
Upgrade
Version history
2.0.2latest on PyPI · released Mar 7, 2019
Audit
Dependencies
No dependency data recorded yet.