Q: going from tuple to comma-separated list?

Damian Menscher menscher+python at uiuc.edu
Tue May 21 01:59:48 EDT 2002


I've got a function that takes an arbitrary number of parameters.
(This is the Gnuplot.plot() function.)  I don't know until runtime
how many things I want to send to it.  So I thought I'd be clever
and do it as
plotdata = ()
for i in range(stuff):
	plotdata = plotdata + (function(i),)
Gnuplot.plot(plotdata)

But the plot function is dying due to:
TypeError: bad argument type for built-in operation

I realize plotdata is a tuple.  So this is the equivalent of me
calling Gnuplot.plot((0, 1, 2))
Note the double parenthesis...  I'm guessing I just need to get
rid of them.  Is there a way to convert from a tuple to its
non-parenthesized equivalent?

Only thing I can think of would be to do it using a utility function:
def remove_parens(tuple)
	if len(tuple)==1: return tuple[0]
	if len(tuple)==2: return tuple[0], tuple[1]
	if len(tuple)==3: return tuple[0], tuple[1], tuple[2], tuple[3]
...etc.  And I'm not even sure that would work...

Surely there's a better way?

CC's appreciated, but not required.

Damian Menscher
-- 
-=#| Physics Grad Student & SysAdmin @ U Illinois Urbana-Champaign |#=-
-=#| 488 LLP, 1110 W. Green St, Urbana, IL 61801 Ofc:(217)333-0038 |#=-
-=#| 1429 DCL, Workstation Services Group, CITES Ofc:(217)244-3862 |#=-
-=#| <menscher at uiuc.edu> www.uiuc.edu/~menscher/ Fax:(217)333-9819 |#=-



More information about the Python-list mailing list