Remove square brackets from a List in Python

avatar

Borislav Hadzhiev

Last updated: Sep 11, 2022

banner

Photo from Unsplash

Remove square brackets from a List in Python #

Use the str.join() method to remove the square brackets from a list, e.g. result = ', '.join(str(item) for item in my_list). The str.join() method will join the list into a string without the square brackets.

main.py
# ✅ remove square brackets from list of strings list_of_strings = ['bobby', 'hadz', 'com'] result = ', '.join(list_of_strings) print(result) # 👉️ bobby, hadz, com # --------------------------------------------- # ✅ remove square brackets from 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.

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

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

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

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

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

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 in Python #

To remove the square brackets from a list:

  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

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.

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'

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.

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.