How to Remove Brackets From List in Python?

As a Python developer, I often find myself needing to format lists for better readability or presentation. I encountered this scenario while working on a data analysis project. In this tutorial, I will share various methods to remove brackets from list in Python with suitable examples and screenshots.

Remove Brackets From List in Python

Before getting into the methods, let’s briefly discuss what lists are in Python. A list is a collection of items that can be of different data types. They are defined by square brackets, like so:

my_list = [1, 2, 3, 'apple', 'banana']

When printed, lists display their elements along with the brackets and commas, as shown below:

print(my_list)  # Output: [1, 2, 3, 'apple', 'banana']

While this format is useful for debugging, it may not be ideal for presentation. Let’s explore how to remove those brackets.

Read Is Python a Compiled Language?

Method 1: Use the join() Function

One of the simplest ways to remove brackets is by converting the list to a string using the Python join() method. This method works well for lists containing strings. Here’s how to do it:

Example:

my_list = ['apple', 'banana', 'cherry']
formatted_string = ', '.join(my_list)
print(formatted_string) 

Output:

apple, banana, cherry

You can see the output in the screenshot below.

remove brackets from list python

Steps:

  1. Use the join() method on a string that will separate the elements (e.g., ', ').
  2. Pass the list as an argument to join().

Advantages:

  • Easy to implement.
  • Customizable separators.

Check out How to Convert Letters to Numbers in Python?

Method 2: Use List Comprehension

If you want to format a list containing mixed data types (like integers and strings), you can use Python list comprehension to convert all elements to strings first.

Example:

my_list = [1, 2, 3, 'apple', 'banana']
formatted_string = ', '.join(str(item) for item in my_list)
print(formatted_string) 

Output:

1, 2, 3, apple, banana

You can see the output in the screenshot below.

python remove brackets from list

Steps:

  1. Use a generator expression inside the join() method.
  2. Convert each item to a string using str(item).

Advantages:

  • Works with mixed data types.
  • Retains the order of elements.

Read How to Pad Numbers with Leading Zeros in Python?

Method 3: Use the str() Function

Another simple method is to convert the entire list to a string and then remove the brackets. This can be done using the Python str() function and string slicing.

Example:

my_list = [1, 2, 3, 'apple', 'banana']
formatted_string = str(my_list)[1:-1]  # Remove first and last character
print(formatted_string) 

Output:

1, 2, 3, 'apple', 'banana'

You can see the output in the screenshot below.

remove square brackets from list python

Steps:

  1. Convert the list to a string using str().
  2. Use slicing to remove the first and last characters.

Advantages:

  • Quick and easy for simple lists.
  • Minimal code required.

Check out How to Check if Input is a Number in Python?

Method 4: Use the itertools.chain Method

For those who prefer a more programmatic approach, you can utilize the itertools module, specifically the chain function in Python, to flatten the list and remove brackets.

Example:

import itertools

my_list = [1, 2, 3, 'apple', 'banana']
formatted_string = ', '.join(itertools.chain(my_list))
print(formatted_string)  # Output: 1, 2, 3, apple, banana

Steps:

  1. Import the itertools module.
  2. Use itertools.chain() to flatten the list and then join the elements.

Advantages:

  • Useful for nested lists.
  • More control over complex data structures.

Read What are Floating Point Numbers in Python?

Conclusion

In this tutorial, I have explained how to remove brackets from list in Python. I discussed four important methods to accomplish this task, such as using the join() function, using list comprehension , using str() function and using the itertools.chain method.

You may like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.