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. Some of the methods, we will discuss are:

  • Remove brackets and commas from list in Python using for loop.
  • Remove brackets from list of lists using join function.
  • Printing list without square brackets using asterick operator.

Method 1: Using for loop

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.

Method 2: using join() function

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.

Method 3: using asterisk ‘*’ operator

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 *