Technology

Print the list of integers in Python in a single line without Brackets

By December 29, 2015 No Comments

So while trying to solve one of the hackerearth’s problems, I came across the necessity to print a series of integers in a list on a single line with space as delimiter without the brackets. normally if you print the list directly, the format would be the data would be enclosed inside square brackets and comma is used as a delimiter.

For example if the list ‘data’ contains 1,2,3,4,5 and you execute the following instruction:

[dt_code]print data[/dt_code]

the output would be[dt_code] [1, 2, 3, 4, 5][/dt_code], in order to achieve the desire functionality the most common way to do is as following:

[dt_code]print ‘ ‘.join(map(str,data))[/dt_code]

when the above instruction is executed the output would be [dt_code]1 2 3 4 5[/dt_code].

Another way to do the same if you’re using Python 3, or appropriate Python 2.x version with from __future__ import print_function then: [dt_code]print(*data, sep=”)[/dt_code]

Naveen

Author Naveen

A Computer Science graduate student, a programmer, photographer, reader and blogger

More posts by Naveen

Leave a Reply