Skip to main content

How to Remove Square Brackets from Python Lists and Strings

This guide explains how to effectively remove square brackets ([]) from the string representation of a Python list and how to remove bracket characters from strings themselves. We'll cover methods using str.join(), string slicing, str.replace(), and regular expressions.

Removing Brackets from List Representation (Converting to String)​

The square brackets seen when printing a list (['a', 'b']) are part of its string representation. To create a string without these brackets, you need to join the elements.

The most Pythonic way to create a string from list elements without brackets is str.join():

list_of_strings = ['tutorial', 'reference', 'com']
result = ', '.join(list_of_strings)
print(result) # Output: tutorial, reference, com
  • ', '.join(...) joins the list elements using , as the separator. You can change the separator (' ', '', '-', etc.).

Handling Non-String List Items​

If your list contains non-string elements, convert them to strings first:

list_of_numbers = [44, 22, 66]
result = ', '.join(str(item) for item in list_of_numbers)
print(result) # Output: 44, 22, 66
  • The generator expression (str(item) for item in ...) converts each number to a string before joining.

Using map() with str.join()​

You can also achieve the conversion using map():

list_of_integers = [44, 22, 66]
result = ', '.join(map(str, list_of_integers))
print(result) # Output: 44, 22, 66

Using String Slicing (For Display Only)​

You can convert the list to its string representation and then slice off the brackets. This is generally less flexible than join() and only affects the visual output.

list_of_strings = ['tutorial', 'reference', 'com']
result = str(list_of_strings)[1:-1] # Convert to string and slice
print(result) # Output: 'tutorial', 'reference', 'com' (Note the inner quotes)

Using str.replace() (For Display Only)​

Similar to slicing, you can use replace() on the string representation:

list_of_strings = ['tutorial', 'reference', 'com']
result = str(list_of_strings).replace('[', '').replace(']', '')
print(result) # Output: 'tutorial', 'reference', 'com'
  • This method, like slicing, operates on the string representation and might include unwanted internal quotes.

Removing Bracket Characters from Strings​

If you have strings that contain bracket characters ([, ], {, }, ( ,)), use these methods:

Using str.replace()​

Chain replace() calls to remove specific brackets:

a_string = '[b]obb{y}'
new_string = a_string.replace('[', '').replace(']', '').replace('{', '').replace('}', '')
print(new_string) # Output: tutorial

Using str.strip() (For Leading/Trailing Brackets)​

Use strip() to remove brackets only from the beginning or end of a string:

a_string = '[{tutorial}]'
new_string = a_string.strip('[]{}') # Remove leading/trailing brackets
print(new_string) # Output: tutorial

Using Regular Expressions (re.sub())​

For more complex patterns or removing multiple types of brackets, use re.sub():

import re

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

# Escape square brackets within the regex character set
new_string = re.sub(r'[()\[\]{}]', '', a_string)
print(new_string) # Output: tutorial
  • r'[()\[\]{}]' defines a character set containing all types of brackets. Note that the square brackets ([ and ]) need to be escaped with a backslash (\).

Removing Brackets from Elements Within a List​

To remove brackets from each string element inside a list:

my_list = ['[tutorial]', '[reference]', '[com]']
result = [item.replace('[', '').replace(']', '') for item in my_list]
print(result) # Output: ['tutorial', 'reference', 'com']
  • The list comprehension iterates through each element and applies the replace() method to remove the brackets.

Flattening a List of Lists (Removing Inner Brackets)​

To combine elements from nested lists into a single list (effectively removing the inner brackets):

my_list = [['tutorial'], ['reference'], ['.', 'com']]
flat_list = [x for sublist in my_list for x in sublist]
print(flat_list) # Output: ['tutorial', 'reference', '.', 'com']
  • The nested list comprehension iterates through the outer list (sublist) and then the inner list (x), collecting all x elements into flat_list.