pythonic way of 'b = list(a); b.append(4)"

S

szabi

Hi!

I have a list of three values and want to call a function with four
parameters. I would like
to write something like:

a = [1, 2, 3]
f(*a, 4)

This is syntactically wrong, so is there a function which appends a
value to a list and
returns the new value, so that I could write something like this:

f(list(a).functional_append(4))

I can't modify 'a'.

Thanks,
Szabi
 
P

Paul Rubin

szabi said:
I have a list of three values and want to call a function with four
parameters. I would like
to write something like:

a = [1, 2, 3]
f(*a, 4)

f(*a+[4]) seems to work.
 
S

Steven D'Aprano

Hi!

I have a list of three values and want to call a function with four
parameters. I would like
to write something like:

a = [1, 2, 3]
f(*a, 4)

This is syntactically wrong, so is there a function which appends a
value to a list and
returns the new value, so that I could write something like this:

f(list(a).functional_append(4))

Sure there is, just not as a list method. Python is a programming
language, you can write any function you like:

def functional_append(L, extra):
"""Return a new list consisting of extra appended to the items of L."""
return L + [extra]

Now call it like this:

f(functional_append(a, 4))

But in fact you can now optimize the functional_append away by just doing
this:

f(*a+[4])

and it will work.
 
P

Peter Otten

szabi said:
I have a list of three values and want to call a function with four
parameters. I would like
to write something like:

a = [1, 2, 3]
f(*a, 4)

This is syntactically wrong, so is there a function which appends a
value to a list and
returns the new value, so that I could write something like this:

f(list(a).functional_append(4))

I can't modify 'a'.

Two more options: if you know the name of the function's fourth parameter
you can mix keyword and positional arguments
.... return "f(a=%r, b=%r, c=%r, d=%r)" % (a, b, c, d)
....
'f(a=1, b=2, c=3, d=42)'

or you can use partial function application:
.... def w(*right):
.... return f(*left+right)
.... return w
....'f(a=1, b=2, c=3, d=42)'

A generalized version of partial() will become part of the standard library
in Python 2.5.

Peter
 
S

szabi

The question was perfectly answered by Heiko Wundram:
f(*(a+[4]))

I know python is not a lawnmower but a programming language. I can
solve a lot of problems, but I prefer short, clear and nice solutions
to long and/or too powerful ones. So, my problem with my solution (b =
list(a); b.append(4)) was the same as with the others' involving
functions.

Actually what I missed is the fact that the + operator works fine with
lists...
With '+' I can solve everything. Ok, _almost_ everything :)

Thanks for everybody!

Szabi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top