Python printing without commas

Example you have a list called names.

names = ["Sam", "Peter", "James", "Julian", "Ann"]
for name in range(len(names)):
    print names[name],

If the list is

l=[1,2,3,4,5]

Printing the list without bracket and commas:

print " ".join(map(str,l))

#1 2 3 4 5

blah = [ [1,2,3], [1,3,2] ]

for bla in blah:
    print ' '.join(map(str, bla))

It's worth noting that map is a bit old-fashioned and is better written as either a generator or list-comp depending on requirements. This also has the advantage that it'll be portable across Python 2.x & 3.x as it'll generate a list on 2.x, while remain lazy on 3.x

So, the above would be written (using a generator expression) as:

for bla in blah:
    print ' '.join(str(n) for n in bla)

Or using string formatting:

for bla in blah:
    print '{} {} {}'.format(*bla)

Number_list = [1, 2, 3, 4, 5]
Print(*Number_list, sep="") # empty quote

Tags:

Python