list parameter of a recursive function

Chris Rebert clp2 at rebertia.com
Thu Oct 7 07:54:56 CEST 2010


On Wed, Oct 6, 2010 at 10:25 PM, TP <Tribulations at paralleles.invalid> wrote:
> Diez B. Roggisch wrote:
>
>> Back to your example: your solution is perfectly fine, although a bit
>> costly and more error-prone if you happen to forget to create a copy.
>> A safer alternative for these cases is using tuples, because they are
>> immutable.
>
> Thanks Diez for your explanation.
> The problem with tuples is that it is not easy to modify them: in my case, I
> need to append a string to the tuple at each recursive step.
> So, it seems to me that I need to write a loop to modify the tuple, because
> on a simple example:
>
>>>> a=("foo", "bar")
>>>> b=(a[0],a[1],"toto")
>>>> b
> (u'foo', u'bar', u'toto')
>
> I do not find any other means to obtain that result for b. With lists, I can
> use ".extend()".
> Am I right?

Nope, sorry:
>>> a = ("foo", "bar")
>>> s = "toto"
>>> b = a + (s,)
>>> b
('foo', 'bar', 'toto')

Cheers,
Chris
--
http://blog.rebertia.com



More information about the Python-list mailing list