Moving to functional programming

George Sakkis george.sakkis at
Sat Jul 12 01:18:34 CEST 2008

On Jul 11, 1:00 pm, James Fassett <ja... at> wrote:
> Hi all,
> Had a simple problem that turned into an interesting solution and I
> thought I would share it here.
> I had a list of tuples that I needed to get the first value from and
> generate a list.
> tuple_list = (
>     ('John', 'Doe'),
>     ('Mark', 'Mason'),
>     ('Jeff', 'Stevens'),
>     ('Bat', 'Man')
>   )
> # what I'd do in C or other procedural languages
> result_list = []
> for item in tuple_list:
>     result_list.append(item[0])
> # the first Pythonic attempt using comprehensions
> result_list = [x[0] for x in tuple_list]
> # the final functional way
> [result_list, _] = zip(*tuple_list)
> I really like how Python allows me to do what I feel is the most
> natural solution (for a seasoned procedural programmer) while allowing
> a satisfying path towards a more functional approach.

First off, what exactly does make you think of the last approach as
"functional" ? It relies on positional arguments, tuple unpacking and
the signature of zip(), none of which are functional in the usual
programming language sense of the word. Second, it is less readable,
robust and efficient than the list comprehension.

An example of a really functional approach without all these drawbacks
would be bearophile's map(itemgetter(0), tuple_list) since
itemgetter() is a high order function (more specifically, a function
that returns another function). The list comprehension is still the
most pythonic approach though.


More information about the Python-list mailing list