Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

How to Print List without Brackets in Python

  • 02 Feb, 2025
  • Com 0
Printing a list without brackets in Python

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:

  1. Using map() with join()
  2. Using the asterisk(*) operator
  3. Using slicing
  4. 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

Using join() with map()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

  1. It provides the flexibility to set any type of delimiter.
  2. You can store it however you want, including writing to a file.
  3. It is efficient and optimized for large lists.

Cons

  1. It requires a string conversion, which not be a suitable option for every use case.
  2. 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

Using the asterisk(*) operator to print a list without brackets

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

  1. It provides quick space/comma-separated output without brackets. Provides a flexible separator (*).
  2. It is a one-liner with no loops or conversions.

Cons

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

Using slicingExample

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

  1. It provides a minimal code for quick removal of parenthesis.
  2. It works directly on the string representation of the list.

Cons

  1. It fails if elements contain commas or brackets (e.g., [“a, b”, 2] → ‘a, b, 2’).
  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

  1. It provides full control over customization.
  2. It conditionally adds delimiters using index checks.

Cons

  1. It still requires a loop, which is not great for each case.
  2. It must handle non-string elements explicitly.

That’s all!

Post Views: 13
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

How to Prepend to List in Python [5 Ways]
Python Find in List: How to Find the Index of an Element in List

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend