DEV Community

Aisha Rajput
Aisha Rajput

Posted on • Updated on

Remove brackets and Quotes from a list in 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 a topic of 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. Items on the list can be duplicated or redundant. A list is always defined inside square [] brackets.

Syntax

list =[item1, item2, item3]
print(list)
Enter fullscreen mode Exit fullscreen mode

Example:Below created a list of “phone” that contains five different “items” that are famous phone companies. The Python list is enclosed with square brackets with double quotation marks on every item that is separated by a comma.
Output as shown in the below code, print a list with a square bracket and single quotations on each item.
Syntax

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

Enter fullscreen mode Exit fullscreen mode

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.
    Syntax

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

Enter fullscreen mode Exit fullscreen mode

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

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 a 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. Syntax
phone = ["Samsung", "Apple", "Huawei", "Nokia", "Windows"]
print(','.join(phone))
#output
Samsung,Apple,Huawei,Nokia,Windows
Enter fullscreen mode Exit fullscreen mode

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.

Syntax

phone = ["Samsung", "Apple", "Huawei", "Nokia", "Windows"]
separator=","
print( separator.join(phone))
#output
Samsung,Apple,Huawei,Nokia,Windows
Enter fullscreen mode Exit fullscreen mode

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

Method 3
The above join method does not work 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, and allows for execution, and transforms all items of a loop into 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 the map, pass two arguments: function str and a list variable “a”. This will help to map integers into the string.

Syntax

a = [1,2,3,4,5]
separator=","
print( separator.join(map(str,a)))
#output
1,2,3,4,5
Enter fullscreen mode Exit fullscreen mode

3. By using For loop

  • First, define a list with their items.
  • create a for loop “p“ that will execute each item from the list “phone” and call the print command will start printing 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.

Syntax

phone = ["Samsung", "Apple", "Huawei", "Nokia", "Windows"]
for p in phone:
     print(p, end=",")
print("\b", end = " ")
print(" ")
#output
Samsung,Apple,Huawei,Nokia,Windows
Enter fullscreen mode Exit fullscreen mode

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.

Syntax

b = ["Samsung", "Apple", "Huawei", "Nokia", "Windows"]
X = ','.join(str(a)for a in b)
print(X)
#output
Samsung,Apple,Huawei,Nokia,Windows

Enter fullscreen mode Exit fullscreen mode

5. translate method ()

This method has limitations to print lists without brackets and quotations in python. in the translate 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.

Syntax

b = ["Samsung", "Apple", "Huawei", "Nokia", "Windows"]
rep {39 : None, 91 : None, 93 :None}
print(str(phone).translate(rep))
#output
Samsung,Apple,Huawei,Nokia,Windows
Enter fullscreen mode Exit fullscreen mode

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

Syntax

a=[1,2,3,4]
print(str(a)[1:-1])
#output
1, 2, 3, 4
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)