I have a list...

D

Damir Hakimov

Hi, All!

say, i have a function:

def f(*b):
print b
return

then i do:
f(3,4,5)
(3, 4, 5)

but i have list f=(3,4,5)
f(l)
((3, 4, 5),)

how can i call f function to result
f(???(b))
(3, 4, 5)

Thanks!
 
A

Albert Hofkamp

Hi, All!

say, i have a function:

def f(*b):
print b
return

then i do:
f(3,4,5)
(3, 4, 5)

but i have list f=(3,4,5)
f(l)
((3, 4, 5),)

how can i call f function to result
f(???(b))
(3, 4, 5)

You mean apply(f,(3,4,5)) ?


Albert
 
U

user

Damir said:
Hi, All!

say, i have a function:

def f(*b):
print b
return

then i do:
f(3,4,5)
(3, 4, 5)

but i have list f=(3,4,5)
f(l)
((3, 4, 5),)

how can i call f function to result
f(???(b))
(3, 4, 5)

Thanks!
You can use the keyword 'type' to check the type of your arguments
and return the appropriate 'format' according to the their types

Regards

Salvatore
 
D

Dialtone

Damir Hakimov said:
say, i have a function:

def f(*b):
print b
return

then i do:
f(3,4,5)
(3, 4, 5)

This is not a list but a tuple.
but i have list f=(3,4,5)
f(l)
((3, 4, 5),)

The standard way to represent a tuple with one element is to put a coma
after that element like ("donald",)
how can i call f function to result
f(???(b))
(3, 4, 5)

If you want this you should use a list which has square brackets [].

But the arguments passed with *b are incapsuleted into a tuple so you should
print something like this:
.... print b[0]
[1, 2, 3]
 
D

Duncan Booth

Hi, All!

say, i have a function:

def f(*b):
print b
return

then i do:
f(3,4,5)
(3, 4, 5)

but i have list f=(3,4,5)
f(l)
((3, 4, 5),)

how can i call f function to result
f(???(b))
(3, 4, 5)
I'm not sure any of the other responses actually answered the question,
which I think was meant to be, given a tuple l=3,4,5 how do you pass that
tuple to the function f so that b simply gets the tuple. The answer is that
you try:
(3,4,5)

If that doesn't work, then you upgrade to a more recent version of Python.
If you (or your users) really can't upgrade you should use 'apply'.
 
B

Bengt Richter

Hi, All!

say, i have a function:

def f(*b):
print b
return

then i do:
f(3,4,5)
(3, 4, 5)

but i have list f=(3,4,5)
f(l)
((3, 4, 5),)

how can i call f function to result
f(???(b))
(3, 4, 5)
Is this what you are looking for? :
... print b
... ((1, 2, 3),)

tup was single arg, but:
(1, 2, 3)

tup got unpacked to make args
([4, 5, 6],)

L was single arg, but:
(4, 5, 6)

L got unpacked similarly, but note that args become tuple b, not a list.

Regards,
Bengt Richter
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top