How to Remove Square Brackets from a List using Python


Python is a very useful software which can be used for many different purposes as per need. The different process for which python can be used are web development, data science, machine learning, and at many other different places where process is to be performed with automation. It has many different features which help us to perform these tasks. One such useful feature of python is python lists. As the name suggest, list contains all the data that you wish to store. It is basically a collection of different types of information.

Different Methods to Remove Square Brackets

Many−a−times, user face an experience where the list item are displayed in a square bracket. In this article we will learn in detail how to remove those brackets to give a better view of your list.

String And Replace Function

One of the simplest method to remove bracket is by using the replace() function after creating the list into strings with the help of str() function. This method makes the work very easy by making the code length shorter and easier to understand.

Example

# List Containing Brackets
bracket_list = ["Jack", "Harry", "Sam", "Daniel", "John"]

# We will use str() and replace() to remove the square brackets
modified_list = str(bracket_list).replace('[', '').replace(']', '')

print(modified_list)

Output

The output of this code will be as follows:

'Jack', 'harry', 'Sam', 'Daniel', 'John'

List Comprehension and Join

This is another simple method in which we first use the list comprehension to convert elements into string and then simply using the join() function to remove the bracket. The list comprehension helps to keep the code short when a new list is to be created by taking the data from an existing list. We can understand the use of list comprehension through the following example:

Example

# Old list with brackets
old_list = ['A', 'B', 'C', 'D', 'E']

# Removing square brackets using list comprehension and join()
modified_list = ', '.join([str(element) for element in old_list])

print(modified_list)

Output

The output of the above code will be:

A, B, C, D, E  

Map Function & Join String Function

In this method of removing the brackets from the list, we will simply use the map function to convert the elements into string and followed by that we will use the join() function to remove the bracket. The map function is normally used to execute a command on each item of a list. We will understand it more clearly with the help of the following example:

Example

# Old list with brackets
old_list = [1, 2, 3, 4, 5]

# using map() to create elements into string and str.join() to remove the brackets
modified_list = ', '.join(map(str, old_list))

print(modified_list)

Output

The output of the above code will be as follows:

1, 2, 3, 4, 5 

Strip Function

This is very simple method to use for small list. Under this method we will first convert the elements into string and then use the strip function to remove the brackets from the list.

Example

# The old list which contains bracket
old_list = ['P', 'Q', 'R', 'S', 'T']

#The elements are first coverted into tring and then strip() function is given the argument to remove the brackets
modified_list = str(old_list).strip('[]')

print(modified_list)

Output

The output of the above code will be as follows:

'P', 'Q', 'R', 'S', 'T'  

re Module

Re module is used to check if a pattern is matched by a particular string. It provides the user an expression facility. In the case we will use the re.sub() function from the RE module to remove the brackets. The re.sub() function is basically used to provide a substitute to a particular element and in this case we will use it to replace the bracket with an empty element.

Example

import re #We first need to import re module to work with it
#many people forget to import re and due to that reason, there is an error in running the code

# Old list with brackets
old_list = [1, 2, 3, 4, 5]

#Using re.sub() function from re module to replace bracket with empty string
modified_list = re.sub(r'[\[\]]', '', str(old_list))

print(modified_list)

Output

The output of the above code will be as follows:

1, 2, 3, 4, 5 

Translate Function

This is complex method for removing brackets from the list of elements. In this method the elements are first converted into a string just like all the other methods but after converting the elements into string a translational table is to be created where it is specified that the brackets are to be removed. We can understand it in a more clear way through the following example:

Example

# Old list with brackets
old_list = [1, 2, 3, 4, 5]

# Converting elements into string and then creating a translational table which provides the argument to remove the bracket
modified_list = str(old_list).translate(str.maketrans('', '', '[]'))

print(modified_list)

Output

The output of the above code will be as follows:

1, 2, 3, 4, 5 

Conclusion

The article represents different ways in which bracket can be removed from a list. Different methods use different functions to remove the bracket. You can use the method of your choice as per your requirement and based upon the complexity of the list. Any different function can be used like the replace function, join function, strip function, map function, re module and the translate function. If the first and last elements are to be removed then the slicing function can also be used by list slicing and creating a new list without any bracket.

Updated on: 01-Aug-2023

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements