How to do [1]*4 for a tuple

Rob Williscroft rtw at freenet.REMOVE.co.uk
Tue Apr 6 09:23:45 EDT 2004


Vineet Jain wrote in news:mailman.375.1081213535.20120.python-
list at python.org:

> I'm not sure I understand why [1]*4 and (1)*4 work differently? [1]*4
> results in [1, 1, 1, 1] while (1)*4 results in 4. There are times when 
> I have to do the following:
> 

>>> (1,) * 4
(1, 1, 1, 1)
>>> 


> '%s some value %s and some other text %s' % (a)*3
> 

>>> a = "<aaa>"
>>> '%s some value %s and some other text %s' % ((a,)*3)
'<aaa> some value <aaa> and some other text <aaa>'



> as apposed to
> 
> '%s some value %s and some other text %s' % (a, a, a)
> 
> In this case I always end up doing
> 
> '%s some value %s and some other text %s' % [a]*3
> 

tuples are comma seperated items, parenthesis just bracket:

>>> b = 1, 3
>>> b
(1, 3)
>>> type( b )
<type 'tuple'>
>>> c = 1,
>>> c
(1,)
>>> type( c )
<type 'tuple'>
>>> 

Rob.
-- 
http://www.victim-prime.dsl.pipex.com/



More information about the Python-list mailing list