Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove square brackets from list in Python? [duplicate]

LIST = ['Python','problem','whatever'] print(LIST) 

When I run this program I get

[Python, problem, whatever] 

Is it possible to remove that square brackets from output?

like image 236
Gregor Gajič Avatar asked Nov 03 '12 09:11

Gregor Gajič


People also ask

How do I get rid of the square brackets when printing a list in Python?

The join() function takes all the elements from an iterable object, like a list, and returns a string with all elements separated by a character specified with the function. Using this method, we can remove the square brackets from a list and separate the elements using a comma or whichever character we desire.

Are square brackets a list in Python?

What is a Python list? A list is an ordered and mutable Python container, being one of the most common data structures in Python. To create a list, the elements are placed inside square brackets ([]), separated by commas.

Why are there two square brackets in Python?

You can either use a single bracket or a double bracket. The single bracket will output a Pandas Series, while a double bracket will output a Pandas DataFrame. Square brackets can also be used to access observations (rows) from a DataFrame.


1 Answers

You could convert it to a string instead of printing the list directly:

print(", ".join(LIST)) 

If the elements in the list aren't strings, you can convert them to string using either repr (if you want quotes around strings) or str (if you don't), like so:

LIST = [1, "foo", 3.5, { "hello": "bye" }] print( ", ".join( repr(e) for e in LIST ) ) 

Which gives the output:

1, 'foo', 3.5, {'hello': 'bye'} 
like image 161
Sebastian Paaske Tørholm Avatar answered Sep 21 '22 11:09

Sebastian Paaske Tørholm