# Remove square brackets from a List or a String in Python

Last updated: Apr 9, 2024
6 min

## #Remove square brackets from a List in Python

Use the `str.join()` method to remove the square brackets from a list.

The `str.join()` method will join the list into a string without the square brackets.

main.py
```Copied!```# โ remove square brackets from a list of Strings

result = ', '.join(list_of_strings)
print(result)  # ๐๏ธ bobby, hadz, com

# ---------------------------------------------

# โ remove square brackets from a list of Integers
list_of_numbers = [44, 22, 66]

result = ', '.join(str(item) for item in list_of_numbers)
print(result)  # ๐๏ธ 44, 22, 66
``````

We used the `str.join()` method to remove the square brackets from a list.

If you need to remove the square brackets from a string, click on the following subheading:

The str.join() method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.

## #Remove the square brackets from a List of Integers

Note that the method raises a `TypeError` if there are any non-string values in the iterable.

If your list contains numbers or other types, convert all of the values to strings before calling `join()`.

main.py
```Copied!```list_of_numbers = [44, 22, 66]

result = ', '.join(str(item) for item in list_of_numbers)
print(result)  # ๐๏ธ 44, 22, 66
``````

We used a generator expression to iterate over the list.

Generator expressions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we use the str() class to convert the number to a string.

The string the `join()` method is called on is used as the separator between the elements.

main.py
```Copied!```list_of_numbers = [44, 22, 66]

result = ' '.join(str(item) for item in list_of_numbers)
print(result)  # ๐๏ธ 44 22 66
``````

If you don't need a separator and just want to join the list's elements into a string, call the `join()` method on an empty string.

main.py
```Copied!```list_of_numbers = [44, 22, 66]

result = ''.join(str(item) for item in list_of_numbers)
print(result)  # ๐๏ธ 442266
``````

## #Remove the square brackets from a List of Integers using map()

You can also use the `map()` function to convert all items in the list to strings before calling `join()`.

main.py
```Copied!```list_of_integers = [44, 22, 66]

result = ''.join(map(str, list_of_integers))
print(result)  # ๐๏ธ 442266
``````

The map() function takes a function and an iterable as arguments and calls the function with each item of the iterable.

## #Remove the square brackets to flatten a two-dimensional List

If you need to remove the square brackets to flatten the list, use a list comprehension.

main.py
```Copied!```my_list = [['bobby'], ['hadz'], ['.', 'com']]

flat_list = [x for xs in my_list for x in xs]
print(flat_list)  # ๐๏ธ ['bobby', 'hadz', '.', 'com']
``````

Alternatively, you can use string slicing.

## #Remove square brackets from a List using string slicing

This is a three-step process:

1. Use the `str()` class to convert the list to a string.
2. Use string slicing to exclude the first and last characters from the string.
3. The string won't contain the square brackets.
main.py
```Copied!```list_of_strings = ['bobby', 'hadz', 'com']

result = str(list_of_strings)[1:-1]
print(result)  # ๐๏ธ 'bobby', 'hadz', 'com'

# ---------------------------------------------

list_of_integers = [44, 22, 66]

result = str(list_of_integers)[1:-1]
print(result)  # ๐๏ธ 44, 22, 66
``````

We used the `str()` class to convert the list to a string and used string slicing to exclude the square brackets.

main.py
```Copied!```list_of_strings = ['bobby', 'hadz', 'com']

print(str(list_of_strings))
``````

The syntax for string slicing is `my_str[start:stop:step]`.

The `start` index is inclusive, whereas the `stop` index is exclusive (up to, but not including).

Python indexes are zero-based, so the first character in a string has an index of `0`, and the last character has an index of `-1` or `len(my_str) - 1`.

We used a `start` index of `1` to exclude the left square bracket and used a `stop` index of `-1` to exclude the right square bracket.

## #Remove square brackets from a List using `str.replace()`

You can also use the `str.replace()` method to achieve the same result.

main.py
```Copied!```list_of_strings = ['bobby', 'hadz', 'com']

result = str(list_of_strings).replace('[', '').replace(']', '')
print(result) # ๐๏ธ 'bobby', 'hadz', 'com'
``````

If you need to remove the square brackets from each item in the list, use the `str.replace()` method.

main.py
```Copied!```my_list = ['[bobby]', '[hadz]', '[com]']

result = [item.replace('[', '').replace(']', '') for item in my_list]
print(result)  # ๐๏ธ ['bobby', 'hadz', 'com']
``````

The str.replace() method returns a copy of the string with all occurrences of a substring replaced by the provided replacement.

The method takes the following parameters:

NameDescription
oldThe substring we want to replace in the string
newThe replacement for each occurrence of `old`
countOnly the first `count` occurrences are replaced (optional)

The method doesn't change the original string. Strings are immutable in Python.

## #Remove brackets from a String in Python

Use multiple calls to the `str.replace()` method to remove the brackets from a string.

main.py
```Copied!```a_string = '[b]obb{y}'

new_string = a_string.replace('[', '').replace(
']', '').replace('{', '').replace('}', '')

print(new_string)  # ๐๏ธ 'bobby'
``````

The example uses multiple calls to the `str.replace()` method to remove the brackets from the string.

The str.replace() method returns a copy of the string with all occurrences of a substring replaced by the provided replacement.

main.py
```Copied!```a_string = '[b]obb{y}'

new_string = a_string.replace('[', '').replace(
']', '').replace('{', '').replace('}', '')

print(new_string)  # ๐๏ธ 'bobby'
``````

The method takes the following parameters:

NameDescription
oldThe substring we want to replace in the string
newThe replacement for each occurrence of `old`
countOnly the first `count` occurrences are replaced (optional)

The method doesn't change the original string. Strings are immutable in Python.

We used an empty string as the replacement character to remove the brackets from the string.

You can chain as many calls to the `str.replace()` method as necessary.

## #Remove brackets from a String using `str.strip()`

If the brackets are only at the start or end of the string, you can also use the `str.strip()` method.

main.py
```Copied!```a_string = '[{bobby}]'

new_string = a_string.strip('][}{')

print(new_string)  # ๐๏ธ bobby
``````

The str.strip() method returns a copy of the string with the specified leading and trailing characters removed.

## #Remove brackets from a string using re.sub()

Alternatively, you can use the `re.sub()` method.

The `re.sub()` method will remove the brackets from a string by replacing them with empty strings.

main.py
```Copied!```import re

a_string = '[b](o)bb{y}'

# ๐๏ธ using backslash to escape square brackets
new_string = re.sub(r'[()\[\]{}]', '', a_string)

print(new_string)  # ๐๏ธ bobby
``````

The re.sub method returns a new string that is obtained by replacing the occurrences of the pattern with the provided replacement.

The first argument we passed to the `re.sub()` method is a regular expression.

The square brackets `[]` are used to indicate a set of characters.

Note that we had to use backslash `\` characters to escape the square brackets inside of the set.

This approach also works if you have a multiline string.

main.py
```Copied!```import re

multiline_string = """[bobby]
(com)"""

new_string = re.sub(r'[()\[\]{}]', '', multiline_string)

# bobby
# com
print(new_string)
``````

You can adjust the characters between the square brackets if you need to remove different types of brackets.

Here is an example that only removes the square brackets from the string.

main.py
```Copied!```import re

a_string = '[b]obby'

new_string = re.sub(r'[\[\]]', '', a_string)

print(new_string)  # ๐๏ธ bobby
``````