Remove square brackets from a List or a String in Python

avatar
Borislav Hadzhiev

Last updated: Feb 21, 2023
5 min

banner

# Table of Contents

  1. Remove square brackets from a List in Python
  2. Remove brackets from a String in Python

# 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
# โœ… remove square brackets from a list of Strings list_of_strings = ['bobby', 'hadz', 'com'] 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

remove square brackets from list

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
list_of_numbers = [44, 22, 66] result = ', '.join(str(item) for item in list_of_numbers) print(result) # ๐Ÿ‘‰๏ธ 44, 22, 66

remove square brackets from list of integers

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
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
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
list_of_integers = [44, 22, 66] result = ''.join(map(str, list_of_integers)) print(result) # ๐Ÿ‘‰๏ธ 442266

remove square brackets from list of integers using map

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

remove square brackets from list using string slicing

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

main.py
list_of_strings = ['bobby', 'hadz', 'com'] # "['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
list_of_strings = ['bobby', 'hadz', 'com'] result = str(list_of_strings).replace('[', '').replace(']', '') print(result) # ๐Ÿ‘‰๏ธ 'bobby', 'hadz', 'com'

remove square brackets from list using str replace

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

main.py
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
a_string = '[b]obb{y}' new_string = a_string.replace('[', '').replace( ']', '').replace('{', '').replace('}', '') print(new_string) # ๐Ÿ‘‰๏ธ 'bobby'

remove brackets from string

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
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
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
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
import re multiline_string = """[bobby] {hadz} (com)""" new_string = re.sub(r'[()\[\]{}]', '', multiline_string) # bobby # hadz # 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
import re a_string = '[b]obby' new_string = re.sub(r'[\[\]]', '', a_string) print(new_string) # ๐Ÿ‘‰๏ธ bobby

If you ever need help reading or writing a regular expression, consult the regular expression syntax subheading in the official docs.

The page contains a list of all of the special characters with many useful examples.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev