251

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.

0

14 Answers 14

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

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

2
  • 66
    If some elements in names aren't strings, use print(', '.join(map(str,name))) instead.
    – LHeng
    Commented Dec 1, 2019 at 9:43
  • 3
    This comment is very useful. Commented Nov 21, 2020 at 21:21
127

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.

4
  • 8
    In python 2.7.6, This is a SyntaxError: invalid syntax. Commented Feb 25, 2016 at 13:01
  • 2
    @FredrickGauss if you add from __future__ import print_function it'll work in python 2 as well. Commented Aug 26, 2017 at 22:07
  • 4
    This is the correct answer as it also works with non-strings.
    – otocan
    Commented Feb 28, 2018 at 16:19
  • best for strings!
    – nexoma
    Commented May 21, 2023 at 4:13
61

General solution, works on arrays of non-strings:

>>> print str(names)[1:-1]
'Sam', 'Peter', 'James', 'Julian', 'Ann'
4
  • 12
    This doesn't meet the stated requirement. Note the OP's example showing the resulting names without quotes.
    – John Y
    Commented Sep 6, 2013 at 20:32
  • 1
    @SteveBennett can you please explain the slicing [1:-1] in your answer. I just started learning python. Thank you.
    – Charan
    Commented Aug 2, 2015 at 7:03
  • 5
    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. Commented Aug 2, 2015 at 13:05
  • Use str(names)[1:-1].replace("'", "") to eliminate the quotes.
    – ingyhere
    Commented Sep 2, 2022 at 14:16
29

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.

0
23

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

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

The other is below

>>> print(', '.join(names))
0
17

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
3
  • 1
    WOW, this is awesome!
    – Carlo
    Commented Feb 6, 2022 at 17:53
  • 1
    Is there something similar for printing within a formatted string? print(f"{*names}") does not work.
    – c0mr4t
    Commented Sep 9, 2022 at 18:04
  • This is fantastic. Simple and straightforward Commented Oct 6, 2023 at 17:21
12

This is what you need

", ".join(names)
1
  • 2
    It wraps the result in quotes. Commented Sep 7, 2017 at 5:43
8

','.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]
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

5

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

4

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
1
  • OP says it must be in a single row.
    – M. Ali
    Commented Nov 22, 2015 at 12:12
2

Try this

print(*name)

I found this from AssemblyAI video on youtube, watch this part

2
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
    – ramen
    Commented Nov 22, 2022 at 23:35
  • 1
    print(*name) is a self-explanatory answer, and this piece of code does the work for the question. I attached the link only to credit AssemblyAI as I got this solution from their website.
    – Ajay
    Commented Nov 23, 2022 at 14:02
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

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.