Welcome, guest | Sign In | My Account | Store | Cart

Notice! PyPM is being replaced with the ActiveState Platform, which enhances PyPM’s build and deploy capabilities. Create your free Platform account to download ActivePython or customize Python with the packages you require and get automatic updates.

Download
ActivePython
INSTALL>
pypm install first

How to install first

  1. Download and install ActivePython
  2. Open Command Prompt
  3. Type pypm install first
 Python 2.7Python 3.2Python 3.3
Windows (32-bit)
2.0.0
2.0.1Never BuiltWhy not?
2.0.0 Available View build log
Windows (64-bit)
2.0.0
2.0.1Never BuiltWhy not?
2.0.0 Available View build log
Mac OS X (10.5+)
2.0.0
2.0.1Never BuiltWhy not?
2.0.0 Available View build log
2.0.1Never BuiltWhy not?
2.0.0 Failed View build log
1.0.2 Failed View build log
Linux (32-bit)
2.0.0
2.0.1Never BuiltWhy not?
2.0.0 Available View build log
1.0.2 Available View build log
2.0.1Never BuiltWhy not?
2.0.0 Failed View build log
1.0.2 Failed View build log
Linux (64-bit)
2.0.1 Available View build log
2.0.0 Available View build log
1.0.2 Available View build log
2.0.1Never BuiltWhy not?
2.0.0 Failed View build log
1.0.2 Failed View build log
2.0.1 Available View build log
2.0.0 Available View build log
 
License
MIT
Depended by
Imports
Lastest release
version 2.0.1 on Sep 20th, 2013

first: The function you always missed in Python

https://secure.travis-ci.org/hynek/first.png

first is an MIT licensed Python package with a simple function that returns the first true value from an iterable, or None if there is none. If you need more power, you can also supply a key function that is used to judge the truth value of the element or a default value if None doesn’t fit your use case.

I’m using the term “true” consistently with Python docs for any() and all() — it means that the value evaluates to true like: True, 1, "foo" or [None]. But not: None, False or 0. In JavaScript, they call this “truthy”.

Examples

A simple example to get started:

System Message: ERROR/3 (<string>, line 24)

Unknown directive type "code-block".

.. code-block:: pycon

   >>> from first import first
   >>> first([0, None, False, [], (), 42])
   42

However, it’s especially useful for dealing with regular expressions in if/elif/else branches:

System Message: ERROR/3 (<string>, line 33)

Unknown directive type "code-block".

.. code-block:: python

   import re

   from first import first


   re1 = re.compile('b(.*)')
   re2 = re.compile('a(.*)')

   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))

The optional key function gives you even more selection power. If you want to return the first even number from a list, just do the following:

System Message: ERROR/3 (<string>, line 54)

Unknown directive type "code-block".

.. code-block:: pycon

   >>> from first import first
   >>> first([1, 1, 3, 4, 5], key=lambda x: x % 2 == 0)
   4

default on the other hand allows you to specify a value that is returned if none of the elements is true:

System Message: ERROR/3 (<string>, line 63)

Unknown directive type "code-block".

.. code-block:: pycon

   >>> from first import first
   >>> first([0, None, False, [], ()], default=42)
   42


Usage

The package consists of one module consisting of one function:

System Message: ERROR/3 (<string>, line 75)

Unknown directive type "code-block".

.. code-block:: python

   from first import first

   first(iterable, default=None, key=None)

This function returns the first element of iterable that is true if key is None. If there is no true element, the value of default is returned, which is None by default.

If a callable is supplied in key, the result of key(element) is used to judge the truth value of the element, but the element itself is returned.

first has no dependencies and should work with any Python available. Of course, it works with the awesome Python 3 everybody should be using.

Alternatives

first brings nothing to the table that wasn’t possible before. However the existing solutions aren’t very idiomatic for such a common and simple problem.

The following constructs are equivalent to first(seq) and work since Python 2.6:

System Message: ERROR/3 (<string>, line 102)

Unknown directive type "code-block".

.. code-block:: python

   next(itertools.ifilter(None, seq), None)
   next(itertools.ifilter(bool, seq), None)
   next((x for x in seq if x), None)

None of them is as pretty as I’d like them to be. The re example from above would look like the following:

System Message: ERROR/3 (<string>, line 111)

Unknown directive type "code-block".

.. code-block:: python

   next(itertools.ifilter(None, (regexp.match('abc') for regexp in [re1, re2])), None)
   next((regexp.match('abc') for regexp in [re1, re2] if regexp.match('abc')), None)

Note that in the second case you have to call regexp.match() twice. For comparison, one more time the first-version:

System Message: ERROR/3 (<string>, line 119)

Unknown directive type "code-block".

.. code-block:: python

   first(regexp.match('abc') for regexp in [re1, re2])

Idiomatic, clear and readable. Pythonic. :)

Background

The idea for first goes back to a discussion I had with Łukasz Langa about how the re example above is painful in Python. We figured such a function is missing Python, however it’s rather unlikely we’d get it in and even if, it wouldn’t get in before 3.4 anyway, which is years away as of yours truly is writing this.

So I decided to release it as a package for now. If it proves popular enough, it may even make it into Python’s stdlib in the end.

History
2.0.1 (2013-08-04)
  • Make installable on systems that don’t support UTF-8 by default.
  • Backward incompatible: Drop support for Python older than 2.6, the previous fix gets too convoluted otherwise. Please don’t use Python < 2.6 anyway. I beg you. N.B. that this is a pure packaging/QA matter: the module still works perfectly with ancient Python versions.
2.0.0 (2012-10-13)
  • pred proved to be rather useless. Changed to key which is just a selector. This is a backward incompatible change and the reason for going 2.0.
  • Add default argument which is returned instead of None if no true element is found.
1.0.2 (2012-10-09)
  • Fix packaging. I get this never right the first time. :-/
1.0.1 (2012-10-09)
  • Documentation fixes only.
1.0.0 (2012-10-09)
  • Initial release.

Credits

“first” is written and maintained by Hynek Schlawack and various contributors:

  • Łukasz Langa
  • Nick Coghlan
  • Vincent Driessen

Subscribe to package updates

Last updated Sep 20th, 2013

Download Stats

Last month:1

What does the lock icon mean?

Builds marked with a lock icon are only available via PyPM to users with a current ActivePython Business Edition subscription.

Need custom builds or support?

ActivePython Enterprise Edition guarantees priority access to technical support, indemnification, expert consulting and quality-assured language builds.

Plan on re-distributing ActivePython?

Get re-distribution rights and eliminate legal risks with ActivePython OEM Edition.