Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print list without brackets in a single row

Tags:

python

list

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.

like image 583
Isuru Avatar asked Oct 02 '22 03:10

Isuru


People also ask

How do I print a list without brackets?

use join() function to print array or without square brackets or commas. The join() function takes an iterable object such as a list, tuple, dictionary, string, or set as an argument and returns a string in which all the elements are joined by a character specified with the function.

How do you remove list brackets in print Python?

Using strip() to Remove Brackets from the Beginning and End of Strings in Python. If your brackets are on the beginning and end of your string, you can also use the strip() function. The Python strip() function removes specified characters from the beginning and end of a string.

How do you print an array element without brackets in Python?

To print a NumPy array without enclosing square brackets, the most Pythonic way is to unpack all array values into the print() function and use the sep=', ' argument to separate the array elements with a comma and a space.

How do you remove brackets and quotes from a list in Python?

Using join function to remove brackets from a list in Python. join() is a built-in function in python. This method takes all the elements from a given sequence. Then it joins all the elements and prints them into a single element.


1 Answers

print(', '.join(names))

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

like image 332
FatalError Avatar answered Oct 16 '22 06:10

FatalError