or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/pypi-first

Return the first true value of an iterable.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/first@2.0.x

To install, run

npx @tessl/cli install tessl/pypi-first@2.0.0

index.mddocs/

first

A simple Python utility function that returns the first truthy value from an iterable. The first package provides a clean, Pythonic solution for finding the first element that evaluates to True, with optional support for custom key functions and default values when no truthy element is found.

Package Information

  • Package Name: first
  • Language: Python
  • Installation: pip install first

Core Imports

from first import first

Access to package metadata:

import first
print(first.__version__)  # '2.0.2'
print(first.__author__)   # 'Hynek Schlawack'

Basic Usage

from first import first

# Simple usage - returns first truthy value
result = first([0, False, None, [], (), 42])
print(result)  # 42

# Returns None if no truthy values found
result = first([0, False, None, [], ()])
print(result)  # None

# Use default value when no truthy element exists
result = first([0, False, None, [], ()], default='not found')
print(result)  # 'not found'

# Use key function to customize truthiness evaluation
result = first([1, 1, 3, 4, 5], key=lambda x: x % 2 == 0)
print(result)  # 4 (first even number)

Capabilities

First Element Selection

Returns the first element from an iterable that evaluates to true, or a default value if none found.

def first(iterable, default=None, key=None):
    """
    Return first element of `iterable` that evaluates true, else return None
    (or an optional default value).

    Parameters:
    - iterable: Any iterable object (list, tuple, generator, etc.)
    - default: Optional default value to return if no truthy element is found (defaults to None)
    - key: Optional one-argument predicate function like that used for filter(). Must be in keyword form.

    Returns:
    First truthy element from the iterable, or `default` if none found

    Examples:
    >>> first([0, False, None, [], (), 42])
    42
    
    >>> first([0, False, None, [], ()]) is None
    True
    
    >>> first([0, False, None, [], ()], default='ohai')
    'ohai'
    
    >>> first([1, 1, 3, 4, 5], key=lambda x: x % 2 == 0)
    4
    """

Package Metadata

Access to package version and author information.

__version__ = "2.0.2"
__author__ = "Hynek Schlawack"
__license__ = "MIT"
__copyright__ = "Copyright 2012 Hynek Schlawack"
__title__ = "first"

Usage Examples

Regular Expression Matching

Common pattern for handling multiple regex attempts:

import re
from first import first

# Define regex patterns
re1 = re.compile('b(.*)')
re2 = re.compile('a(.*)')

# Find first matching pattern
m = first(regexp.match('abc') for regexp in [re1, re2])
if not m:
    print('no match!')
elif m.re is re1:
    print('re1', m.group(1))
elif m.re is re2:
    print('re2', m.group(1))

Filtering with Custom Keys

Use key functions to find first element matching specific criteria:

from first import first

# Find first even number
numbers = [1, 1, 3, 4, 5]
result = first(numbers, key=lambda x: x % 2 == 0)
print(result)  # 4

# Find first boolean value
mixed_values = [(), 0, False, 3, []]
result = first(mixed_values, key=lambda x: isinstance(x, bool))
print(result)  # False

# Find first integer value
result = first(mixed_values, key=lambda x: isinstance(x, int))
print(result)  # 0

Providing Default Values

Handle cases where no truthy element exists:

from first import first

# Empty iterables
result = first([], default=42)
print(result)  # 42

# All falsy values
result = first([0, False, []], default=3.14)
print(result)  # 3.14

# No match with key function
result = first([1, 3, 5], key=lambda x: x % 2 == 0, default='no even numbers')
print(result)  # 'no even numbers'