219

I have a list in Python e.g.

names = ["Sam", "Peter", "James", "Julian", "Ann"]

I want to print the array in a single line without the normal " []

names = ["Sam", "Peter", "James", "Julian", "Ann"]
print (names)

Will give the output as;

["Sam", "Peter", "James", "Julian", "Ann"]

That is not the format I want instead I want it to be like this;

Sam, Peter, James, Julian, Ann

Note: It must be in a single row.

Bhargav Rao
  • 45,811
  • 27
  • 120
  • 136
Isuru
  • 2,804
  • 5
  • 29
  • 38

13 Answers13

321
print(', '.join(names))

This, like it sounds, just takes all the elements of the list and joins them with ', '.

Jean-François Fabre
  • 131,796
  • 23
  • 122
  • 195
FatalError
  • 50,375
  • 14
  • 97
  • 115
103

Here is a simple one.

names = ["Sam", "Peter", "James", "Julian", "Ann"]
print(*names, sep=", ")

the star unpacks the list and return every element in the list.

Steve Bennett
  • 98,925
  • 29
  • 148
  • 200
Jianru Shi
  • 1,056
  • 1
  • 7
  • 4
58

General solution, works on arrays of non-strings:

>>> print str(names)[1:-1]
'Sam', 'Peter', 'James', 'Julian', 'Ann'
Steve Bennett
  • 98,925
  • 29
  • 148
  • 200
  • 11
    This doesn't meet the stated requirement. Note the OP's example showing the resulting names without quotes. – John Y Sep 06 '13 at 20:32
  • 1
    @SteveBennett can you please explain the slicing [1:-1] in your answer. I just started learning python. Thank you. – Charan Aug 02 '15 at 07:03
  • 4
    `str(names)` generates a string like `['Sam', 'Peter'...'Ann']`. We don't want the square brackets, hence slice from the 2nd character to the second last character. – Steve Bennett Aug 02 '15 at 13:05
26

If the input array is Integer type then you need to first convert array into string type array and then use join method for joining with , or space whatever you want. e.g:

>>> arr = [1, 2, 4, 3]
>>> print(", " . join(arr))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: sequence item 0: expected string, int found
>>> sarr = [str(a) for a in arr]
>>> print(", " . join(sarr))
1, 2, 4, 3
>>>

Direct using of join which will join the integer and string will throw error as show above.

krishna Prasad
  • 3,187
  • 1
  • 30
  • 41
21

There are two answers , First is use 'sep' setting

>>> print(*names, sep = ', ')

The other is below

>>> print(', '.join(names))
lyerox
  • 311
  • 2
  • 5
12

This is what you need

", ".join(names)
user278064
  • 9,778
  • 1
  • 32
  • 46
8

try to use an asterisk before list's name with print statement:

names = ["Sam", "Peter", "James", "Julian", "Ann"]  
print(*names)

output:

Sam Peter James Julian Ann
7

','.join(list) will work only if all the items in the list are strings. If you are looking to convert a list of numbers to a comma separated string. such as a = [1, 2, 3, 4] into '1,2,3,4' then you can either

str(a)[1:-1] # '1, 2, 3, 4'

or

str(a).lstrip('[').rstrip(']') # '1, 2, 3, 4'

although this won't remove any nested list.

To convert it back to a list

a = '1,2,3,4'
import ast
ast.literal_eval('['+a+']')
#[1, 2, 3, 4]
Vineeth Sai
  • 3,262
  • 7
  • 20
  • 31
6

For array of integer type, we need to change it to string type first and than use join function to get clean output without brackets.

    arr = [1, 2, 3, 4, 5]    
    print(', '.join(map(str, arr)))

OUTPUT - 1, 2, 3, 4, 5

For array of string type, we need to use join function directly to get clean output without brackets.

    arr = ["Ram", "Mohan", "Shyam", "Dilip", "Sohan"]
    print(', '.join(arr)

OUTPUT - Ram, Mohan, Shyam, Dilip, Sohan

Martin
  • 89
  • 1
  • 7
4

print(*names)

this will work in python 3 if you want them to be printed out as space separated. If you need comma or anything else in between go ahead with .join() solution

pramodpxi
  • 831
  • 6
  • 12
3

You need to loop through the list and use end=" "to keep it on one line

names = ["Sam", "Peter", "James", "Julian", "Ann"]
    index=0
    for name in names:
        print(names[index], end=", ")
        index += 1
Tofu Warrior
  • 386
  • 3
  • 10
1

I don't know if this is efficient as others but simple logic always works:

import sys
name = ["Sam", "Peter", "James", "Julian", "Ann"]
for i in range(0, len(names)):
    sys.stdout.write(names[i])
    if i != len(names)-1:
        sys.stdout.write(", ")

Output:

Sam, Peter, James, Julian, Ann

Fredrick Gauss
  • 5,087
  • 1
  • 25
  • 43
0

The following function will take in a list and return a string of the lists' items. This can then be used for logging or printing purposes.

def listToString(inList):
    outString = ''
    if len(inList)==1:
        outString = outString+str(inList[0])
    if len(inList)>1:
        outString = outString+str(inList[0])
        for items in inList[1:]:
            outString = outString+', '+str(items)
    return outString
Thennan
  • 606
  • 3
  • 12