Python Print List without Brackets

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

Following are quick examples of how to display a list without brackets.


# Quick examples of printing lists without brackets

# Print list with space separator & without brackets
print(*numbers)

# Print with comma separator & without brackets
print(*numbers, sep = ",") 

# Printing numbers as a string
print(' '.join(map(str, numbers)))

# Using list comprehension
[print(i, end=' ') for i in numbers] 

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.


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

python print lists without brackets

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.

In the second example, I used the list of strings hence map() was not used.


# Create Numbers List
numbers = [11,12,13,14,15,16,17]

# Printing numbers as a string
print(' '.join(map(str, numbers))) 

# Output:
# 11 12 13 14 15 16 17

# 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 

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

NNK

SparkByExamples.com is a Big Data and Spark examples community page, all examples are simple and easy to understand and well tested in our development environment Read more ..

Leave a Reply