Brackets are essential for Python to understand the data structure, but they can make the output less visually appealing or harder to read. When you are exchanging the data with other systems and your data is in the list format, you might reformat your data in such a way that it does not include parenthesis or brackets.
If you use the print() function directly to print the list, it will print it with brackets which is not the main goal.
Here are four ways to print a list without brackets in Python:
- Using map() with join()
- Using the asterisk(*) operator
- Using slicing
- Using loop-through elements
Method 1: Using map() with join()
Using the .map() method, you can convert each element of the list to a string, and using the .join() method, we can combine them into a single string. Then when you print that result, there will be no brackets.
This method is helpful in generating CSV lines or formatted log messages.
Visual Representation
Example
my_list = [9, 18, 27, 36, 45] print("Printing list With Brackets: ", my_list) no_brackets = ' '.join(map(str, my_list)) print("Printing list Without Brackets: ", no_brackets) # Printing list With Brackets: [9, 18, 27, 36, 45] # Printing list Without Brackets: 9 18 27 36 45
Pros
- It provides the flexibility to set any type of delimiter.
- You can store it however you want, including writing to a file.
- It is efficient and optimized for large lists.
Cons
- It requires a string conversion, which not be a suitable option for every use case.
- It will struggle if you come across a nested list, like lists within a list.
Method 2: Using the asterisk(*) operator
The easiest way is to use the * operator in the print() function to unpack the list elements and print them separated by spaces. Depending on your requirements, you can pass a separator, such as a space, newline, or tab.
Visual Representation
Example
my_list = [9, 18, 27, 36, 45] print("Printing list With Brackets: ", my_list) print("Printing list Without Brackets: ", *my_list) # This line will have a comma between each number and the next, but not after the last number print("Without Brackets and comma separator :") print(*my_list, sep=", ") # Printing list With Brackets: [9, 18, 27, 36, 45] # Printing list Without Brackets: 9 18 27 36 45 # Without Brackets and comma separator : # 9, 18, 27, 36, 45
Pros
- It provides quick space/comma-separated output without brackets. Provides a flexible separator (*).
- It is a one-liner with no loops or conversions.
Cons
- It provides limited control over formatting.
Method 3: Using slicing
In this string manipulation method, we first convert a list to a string using the str() function. Then, by slicing it, we can extract everything except the first and last characters, which are the brackets. So, the final output is the elements between the brackets, excluding the brackets.
Visual Representation
Example
my_list = [9, 18, 27, 36, 45] print("Printing list With Brackets: ", my_list) no_brackets = str(my_list)[1:-1] print("Printing list Without Brackets: ", no_brackets) # Printing list With Brackets: [9, 18, 27, 36, 45] # Printing list Without Brackets: 9 18 27 36 45
Pros
- It provides a minimal code for quick removal of parenthesis.
- It works directly on the string representation of the list.
Cons
- It fails if elements contain commas or brackets (e.g., [“a, b”, 2] → ‘a, b, 2’).
- Ugly for nested lists because then it will include nested brackets in the output.
Method 4: Loop through elements
You can loop through the elements and print each one, specifying the end parameter to control what is printed after each element. The enumerate() function tracks the index (i) to avoid adding a separator after the last element.
Example
my_list = [9, 18, 27, 36, 45] print("Printing list With Brackets: ", my_list) print("Printing list Without Brackets: ") for i, item in enumerate(my_list): print(item, end=', ' if i < len(my_list)-1 else '\n') # Printing list With Brackets: [9, 18, 27, 36, 45] # Printing list Without Brackets: # 9, 18, 27, 36, 45
Pros
- It provides full control over customization.
- It conditionally adds delimiters using index checks.
Cons
- It still requires a loop, which is not great for each case.
- It must handle non-string elements explicitly.
That’s all!