Help with searching a list

John Hunter jdhunter at ace.bsd.uchicago.edu
Wed Nov 13 16:09:49 EST 2002


>>>>> "Wojtek" == Wojtek Walczak <gminick at hacker.pl> writes:

    Wojtek> So, looks fine, but, is there any way of declaring 'b'
    Wojtek> list within flatten function?  

Probably the best way is to use a generator, as in the cookbook example.  

One problem with your version is that it restricts the input sequences
to be lists.  For example, it fails on

a=[(1,[2,[3,[4,[5,[6,7]]]]]),[8,9], 10]
print flatten(a)
[(1, [2, [3, [4, [5, [6, 7]]]]]), 8, 9, 10]

That is why the cookbook example does the is_string_like and iterable
song and dance, to allow the recipe to work on any (including user
defined) iterable sequences.

You can read up on generators at
http://www.python.org/peps/pep-0255.html.

Cheers,
John Hunter




More information about the Python-list mailing list