How to Print a List Without Square Brackets in Python

This tutorial is about How to Print a List without Square Brackets in Python. We are assuming that you’re familiar with the basic concepts of lists. For more understanding, read Python List Tutorials. There are various techniques to print lists without showing square brackets which are explained below in detail.

3 Methods to print a list without square brackets in Python

There are three methods to print a list without square brackets in Python Programming.

  1. Use for loop.
  2. Use join() Function.
  3. Use asterick operator.

Use for loop to print a list without brackets or commAs

One of the simplest methods that come into mind is to print the list elements using for loop. Using for loop, traverse through list elements one by one and print them with commas in between them. For example:

fruits = ["Apple", "Mango", "Orange", "Guava", "Peach"]

for item in fruits:

  print(item, end=" ")

Output:

Apple Mango Orange Guava Peach

In the code snippet shown above, the for loop iterates and prints item of lists on every iteration. The end argument inserts space after each element. You can also separate all items with commas or any other character by specifying it in the end argument.

use join() function to print array or without square brackets or commas

The join() function takes an iterable object such as a list, tuple, dictionary, string, or set as an argument and returns a string in which all the elements are joined by a character specified with the function. For example: Suppose you have a list consisting of the name of fruits as an element and you want to print the elements of the list. We can join these names of fruits with commas between them using the join method.

fruits = ['apple','mango','banana','gauva']

print(', '.join(fruits))

Output:

apple,mango,banana,gauva

Similarly, if we replace the comma in the above example with a space, then the above code will print names of fruits separated with a space between them instead of commas.

fruits = ['apple','mango','banana','gauva']

print(' '.join(fruits))

Output:

apple mango banana gauva

This method only works with a list of strings and will fail if the list contains integer or float values. For lists containing integers, first, convert the list into the string using map() function then use join function. The map() function takes two arguments a function and an iterable. It maps all the items of the iterable to the specified function mentioned in the first argument.

list1 = [1,2,3,4,5,6,7,8,9,10]

print(', '.join(map(str, list1))) 

Output:

1, 2, 3, 4, 5, 6, 7, 8, 9, 10

In the above example, the map() function converts all the items of the list into a string datatype which are then joined with commas between them.

use asterisk ‘*’ operator to print a list without square brackets

You can unpack list elements using an asterisk(*) operator. This operator is used to unpack elements of iterable objects. As a list is also an iterable object, therefore we can unpack list elements using this operator and print them without the square brackets. For example

fruits = ['apple','mango','banana','gauva']
print(*fruits, sep = ' ')

Output:

apple mango banana gauva

This method works for all integers, floating numbers, and strings. You can also insert any character between the elements by passing it in the ‘sep’. For example:

list1 = [1,2,3,4,5,6,7,8,9,10]

print(*list1, sep = ', ')

Output:

1, 2, 3, 4, 5, 6, 7, 8, 9, 10

If you want to know more about Lists in Python, see our other tutorials. If you have any feedback, let us know. See more Python Tutorials

Leave a Comment

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