Moving to functional programming

sturlamolden sturlamolden at yahoo.no
Fri Jul 11 14:16:37 EDT 2008


On Jul 11, 12:00 pm, James Fassett <ja... at reggieband.com> wrote:

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

Here are some various 'functional' solutions. Pick the one that fits
your problem best:

result_list = [fn for fn,ln in tuple_list]

result_generator = (fn for fn,ln in tuple_list)

result_list = map(lambda (fn,ln): fn, result_list)

result_generator = itertools.imap(lambda (fn,ln): fn, result_list)





More information about the Python-list mailing list