[Tutor] Fwd: List of tuples

Alan Gauld alan.gauld at yahoo.co.uk
Wed Apr 20 07:07:31 EDT 2016


On 20/04/16 06:52, isaac tetteh wrote:

>> Thanks things are working good now. The only problem is 
>> i want to print the for loop output on one line instead of on each line.
>> Example [1,2,3,4,5]
>> Output 
>> 1 2 3 4 5 
>> I would to do this in Jinja 

I don;t know what Jinja is but...

When you say 'print', where do you want to print it?
It seems you are using Flask so presumably the output goes to a web
page? So presumably you really want to output a string? Or do you
literally want to print to the console?

If you want a string from a list of values you can use join():

s = ' '.join([str(n) for n in outputList])

If you want to print to console you can use

for n in outputList:
   print n,     # note the comma

in Python v2 or

for n in outputList:
    print (n, end=' '))

in Python v3

or just combine the techniques:

s = ' '.join([str(n) for n in outputList])
print(s)


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list