Python printing without commas

How to print lists without brackets and commas?

I have a list of permutations like this:

[1, 2, 3] [1, 3, 2] etc.. 

I want to print them as follows: 1 2 3

+7
source share
7 answers
 blah = [ [1,2,3], [1,3,2] ] for bla in blah: print ' '.join(map(str, bla)) 

It is worth noting that map bit old-fashioned and better written as a generator or list-comp depending on requirements. This also has the advantage of being portable across Python 2.x and 3.x, as it will generate a list at 2.x, remaining lazy at 3.x

So, above it 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) 
+23
source

If the list

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

Printing the list without brackets and commas:

 print " ".join(map(str,l)) #1 2 3 4 5 
+3
source

For example, you have a list with names.

 names = ["Sam", "Peter", "James", "Julian", "Ann"] for name in range(len(names)): print names[name], 
+1
source
 In [1]: blah = [ [1,2,3], [1,3,2] ] In [2]: for x in blah: ...: print x[0],x[1],x[2] ...: 1 2 3 1 3 2 In [3]: 
0
source
 n = int(input()) i = 1 x = [] while (i <= n): x.append(i) i = i+1 print(*x, sep='') 

Assuming n is a number entered by the user, you can start the loop and add the values ​​of i to n in the list x. at the end, you can print the array (* x) using nothing ('') to separate the values. this saves your values ​​in their original format, in this case a number.

0
source

You can do it.

The code:

 list = [[1, 2, 3], [3, 2, 1]] for item in list: print("{}".format(item, end="\n") 

Result:

 [1, 2, 3] [3, 2, 1] 
0
source
 temp = [[2,3,5],[2,5,3]] for each_temp in temp: if isinstance(each_temp,list): for nested_temp in each_temp: print nested_temp 
-one
source

All Articles