printing list
Dan Sommers
me at privacy.net
Sun May 7 18:54:07 EDT 2006
On Sun, 07 May 2006 18:16:22 -0400,
Mel Wilson <mwilson-to at sympatico.ca> wrote:
> Tim Chase wrote:
>> compboy wrote:
>>
>>> How do you print elements of the list in one line?
>>>
>>> alist = [1, 2, 5, 10, 15]
>>>
>>> so it will be like this:
>>> 1, 2, 5, 10, 15
>>
>>
>> >>> print ', '.join(alist)
>> 1, 2, 5, 10, 15
> ???
> Python 2.4.2 (#1, Jan 23 2006, 21:24:54)
> [GCC 3.3.4] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>>>> a=[1,2,3,4,5]
>>>> print ', '.join (a)
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> TypeError: sequence item 0: expected string, int found
>>>> print ', '.join ('%d'%x for x in a)
> 1, 2, 3, 4, 5
Or one of:
print ', '.join(str(x) for x in a)
print ', '.join(map(str, a))
both of which work if the list contains non-integer elements.
Regards,
Dan
--
Dan Sommers
<http://www.tombstonezero.net/dan/>
"I wish people would die in alphabetical order." -- My wife, the genealogist
More information about the Python-list
mailing list