How to print the list without square brackets? When you display the lists by default it displays with square brackets, sometimes you would be required to display the list or array in a single line without the square brackets []. There are multiple ways to achieve this. For example, using *
operator, join()
, list comprehension e.t.c
Related: Different ways to Print Lists
1. Quick Examples of Displaying Lists without Brackets
If you are in a hurry, below are some quick examples of how to display a list without brackets.
# Quick examples of printing lists without brackets
# Example 1: Print list with space separator
# Without brackets
print(*numbers)
# Example 2: Print with comma separator
# Without brackets
print(*numbers, sep = ",")
# Example 3: Printing numbers as a string
print(' '.join(map(str, numbers)))
# Example 4: Using list comprehension
[print(i, end=' ') for i in numbers]
# Example 5: Using str()
print(str(numbers)[1:-1])
2. Print Lists without Brackets
We can use the *
operator to print elements of the list without square brackets and with a space separator. You can also use this to display with comma, tab, or any other delimiter.
The *numbers
syntax unpacks the elements of the numbers
list, and the print
function then receives them as separate arguments. This effectively prints the list without brackets and with a space separator between elements.
# Create Numbers List
numbers = [11,12,13,14,15,16,17]
print(numbers)
# Using * Operator
# Print list with space separator
print(*numbers)
# Print comma separator
print(*numbers, sep = ",")
# Print comma with additional space
print(*numbers, sep = ", ")
Yields below output. Here, the star(*) unpacks the list and returns every element in the list.
So, this approach using the *
operator is a concise way to achieve the desired result of printing a list without brackets and with a space separator.
3. Use Join to Print List without Brackets
If you wanted to print the list as a string without square brackets, you can use the join() to combine the elements into a single string. The join()
can be used only with strings hence, I used map()
to convert the number to a string.
You can use the join
method to concatenate the elements of the list into a string and then print the result without brackets. For instance, the map(str, numbers)
part converts each element of the list to a string, and then ' '.join(...)
concatenates these strings with a space as the separator. The result is printed without brackets
# Create Numbers List
numbers = [11,12,13,14,15,16,17]
# Using join to print list without brackets
result = ' '.join(map(str, numbers))
print("Printing list without brackets:\n",result)
# Output:
# Printing list without brackets:
# 11 12 13 14 15 16 17
In the second example, I used the list of strings hence map()
was not used. It creates a list named mylist
containing string elements (‘Apple’, ‘Mango’, ‘Guava’, ‘Grape’), then uses the join
method to concatenate these strings into a single string separated by a space, and finally, it prints the result.
# Printing string as a single string
mylist = ['Apple','Mango','Guava','Grape']
mystring = ' '.join(mylist)
print(mystring)
# Output:
# Apple Mango Guava Grape
4. Print List Comprehension
You can also use list comprehension to display the list in a single line without square brackets. Here, for is used to get one element at a time from the list, and print()
is used to display the element.
# Using list comprehension
[print(i, end=' ') for i in numbers]
# Output:
# 11 12 13 14 15 16 17
5. Using str() Function
The str()
function converts the numbers list to a string with square brackets and each element is separated by space. To remove the brackets use [1:-1]
, meaning remove the first and last character from the string.
# Using str()
print(str(numbers)[1:-1])
# Output:
# 11 12 13 14 15 16 17
Frequently Asked Questions on Python Print List without Brackets
To print a Python list without brackets, you can use the join
method along with the print
function. For example, the map(str, my_list)
part converts each element of the list to a string, and then ' '.join(...)
concatenates these strings with a space as the separator.
You can achieve the same result without using the join
method by using a loop to iterate through the elements of the list and print them without brackets.
an alternative to using join
is to use the *
operator with the print
function. The *
operator can be used to unpack the elements of a list and pass them as separate arguments to a function, such as print
. Using *
to unpack the list elements directly in the print
function can be a concise and readable way to achieve the goal of printing a list without brackets.
To print a list without brackets and on the same line, you can use the print
function with the end
parameter set to an empty string. For example, the end=' '
parameter specifies that a space character should be printed at the end of each element instead of the default newline character. This effectively prints the elements of the list on the same line, separated by spaces.
List comprehension is typically used to create a new list, but it can also be used to perform actions for each element in a list without creating a new list. If your goal is to print each element without brackets, you can use list comprehension for that purpose.
You can use f-strings to format the output without brackets. For example, the f'{x}'
expression within the generator comprehends is an f-string that converts each element of the list to a string. The join
method concatenates these strings with a space as the separator.
Conclusion
In this article, you have learned different ways to print lists in Python without brackets. Learned to use *
operator, print a list as a string, using list comprehension.
Related Articles
- Get Index of Max of List in Python
- Python Remove None From List
- Convert String to List in Python
- Filter Elements from Python List
- Python list.reverse() Method
- Convert Python List to JSON Examples
- How to Update List Element in Python?
- Python Remove Empty Strings from List
- Sort List of Dictionaries by Value in Python?
- Python Replace Values in List With Examples
- Remove Multiple Items from List Python
- Count Occurrences of Item in Python List
- Python Remove a Trailing New Line
- Python Print without a Newline
- Python print() Function With Examples