Remove Brackets from list Python

Python is a simple syntax language that contains multiple methods to print a list, a dictionary, or a tuple. We are focusing on a list and covering how to remove brackets and quotations/commas from a …

remove brackets from list python

Python is a simple syntax language that contains multiple methods to print a list, a dictionary, or a tuple. We are focusing on a list and covering how to remove brackets and quotations/commas from a list in python.

First, we should know:

 What is a list in Python?

A python list is a mutable(changeable) data structure that stores data in an ordered form. Item of the list can be duplicated or redundant. A list is always defined inside square [] brackets.

Here is the syntax of the python list

list =[“item1”, “item2”, “item3”]
print(list)

Example: Below created a list of “phone” containing five different “items” that are famous phone companies. Python list is enclosed with square brackets with double quotation marks on every item separated by a comma.

As shown in the below code, print a list with a square bracket and single quotations on each item.

phone = ["Samsung","Apple","Huawei","Nokia","Windows"]
print(phone)

Output

['Samsung', 'Apple', 'Huawei', 'Nokia', 'Windows']

What if we want to remove brackets from the list? In this situation, the logical question arises:  How to print a list without brackets in python? What type of functions can help to remove brackets and quotations from the list in Python?

 Here are five easy methods to remove brackets from the list.

  1. * Operator with sep method
  2. join() method
  3. for loop
  4. join with for loop
  5. translate method ()

1. By using * operator with sep method.

*  Operator unpacks each item of the ordered list within one command without indexing each item of the list one by one. 

  • First, define the list of items
  • Use * operator to print each item of the list as a string and sep will separate each item with a comma.

Limitation of sep method: it cannot print a nested list. It can only print limited types of lists.

phone = ["Samsung","Apple","Huawei","Nokia","Windows"]
print(*phone, sep=",")

Output

Samsung,Apple,Huawei,Nokia,Windows

2. Join method: convert list to string.  It joins the items of an iteration.

we can use the join() function in multiple ways. Below are few examples of them

Method 1

  • First, define the list of items.
  • define a “,” inside the print command. 
  • Join converts list to the string that items will separate by commas.
phone = ["Samsung","Apple","Huawei","Nokia","Windows"]
print(','.join(phone))

Output

Samsung,Apple,Huawei,Nokia,Windows

Method 2

  • First, define the list of items.
  • define a separator in which we store “,” as separator value 
  • Call separator with join to execute the list. Join convert list to the string that items will separate by commas.

Note: we can store different types of punctuations inside the separator.

phone = ["Samsung","Apple","Huawei","Nokia","Windows"]

separator = ","
print(separator.join(phone))

Output

Samsung,Apple,Huawei,Nokia,Windows

Method 3

The above join method does not works on integers because it only converts a list into a string that contains characters. 

Print output for input integers , we use map() function inside join(). Mapping of a function works on integer input, allows to execute and transform all items of a loop in a new iterable without using a for a loop. 

  • First, define the list of items.
  • define a separator in which we store “,” as separator value 
  • Call separator with join to execute the list. Join convert list to the string that items will separate by commas.

Inside map, pass two arguments: function str and a list variable “a.” This will help to map integers into the string.

a = [1,2,3,4,5]
separator = ","
print(separator.join(map(str,a)))

Output

1,2,3,4,5

3. By using For loop

  • First, define a list with their items.
  • create a for loop  “p“ that will execute each item from list  “phone” and call print command will start print each comma-separated item from the next line without brackets commas and quotations. 
  • applying comma and space inside the print command of for loop that will print output as a comma-separated list.
phone = ["Samsung","Apple","Huawei","Nokia","Windows"]
for p in phone:
  print(p , end = ",")\
print("\b" , end = " ")
print(" ")

Output

Samsung,Apple,Huawei,Nokia,Windows

4. For loop with join method

Join function joins the list element, and the output will be a string. We use different techniques to split output strings into the list or comma-separated forms.

  • Define a list with their items.
  • use join function that converts a list into a string.
  • generate a for loop “a” that will retrieve each item of defined list “b”.
  • for loop inside join is stores in “X” and call for the print.
b = ["Samsung","Apple","Huawei","Nokia","Windows"]
X = ','.join( str(a) for a in b)
print(X)

Output

Samsung,Apple,Huawei,Nokia,Windows

5. Translate method ()

This method has limitations to print lists without brackets and quotations in python. In the translation method, specified characters are replaced with the described

character. The character must be specified inside a  table or dictionary.

  • Create a variable named rep
  • Targets   “[“,   “ and  “]” by using their ASCII code and set their values None.
  • Print variable by using translate functions.
phone = ["Samsung","Apple","Huawei","Nokia","Windows"]
rep = {39: None, 91: None , 93: None}
print(str(phone).translate(rep))

Output

Samsung,Apple,Huawei,Nokia,Windows

This is another way to print a list that only contains an integer. Print a list of integers converting int into a string.

a = [1,2,3,4]
print(str(a)[1:-1])

Output

1,2,3,4

Conclusion

There are multiple methods to remove brackets and quotations from a list in python. In this article, we discussed five unique methods to print a list without brackets and quotations.

Leave a Comment