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. There are various techniques to print lists without showing square brackets which are explained below in detail.

To Print a list without Square Brackets in Python, you can use for loop, join() Functions, and asterisk ‘*’ operator. Using for loop, traverse through list elements one by one and print them with commas in between them.

1) Use for loop to print a list without brackets or commas

One of the simplest methods that come to 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.

2) Use the Join() Function to print an array 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 the 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 the map() function then use the 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 is then joined with commas between them.

3) Use the Asterisk ‘*’ operator to print a list without square brackets

To print a list without square brackets in Python, there are multiple techniques available. One approach is to use the asterisk (*) operator. The asterisk operator can be used to unpack the elements of a list and print them without the 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 lists containing strings, integers, or floating-point numbers. You can also specify a delimiter by passing it as the ‘sep’ argument. Additionally, list comprehension, the join() function, and the str() function can be used for printing lists without square brackets in Python.”

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

4) Use List Comprehension

List Comprehension can also be used to print the numbers without square brackets in Python.

In this example, the list comprehension [str(num) for num in numbers] creates a new list where each element of the numbers list is converted to a string. The resulting list is ['1', '2', '3', '4', '5'].

Then, the join() method is used to concatenate the elements of the new list with a comma and a space as the delimiter, resulting in the string “1, 2, 3, 4, 5”. Finally, the concatenated string is printed without the square brackets.

numbers = [1, 2, 3, 4, 5]

# Create a new list comprehension to convert each element to a string
numbers_str = [str(num) for num in numbers]

# Use the join() method to concatenate the elements with a delimiter
result = ", ".join(numbers_str)

# Print the resulting string without square brackets
print(result)
# Output:
#1 2 3 4 5 6 7 

5) Use Str Function

To print a list without square brackets using the str() function, you can follow these steps:

  1. Define your list of numbers.
  2. Convert the list to a string using the str() function.
  3. Remove the square brackets from the string using string slicing.
numbers = [1, 2, 3, 4, 5]

# Convert the list to a string
numbers_str = str(numbers)

# Remove the square brackets using string slicing
numbers_str_without_brackets = numbers_str[1:-1]

# Print the resulting string without square brackets
print(numbers_str_without_brackets)
1, 2, 3, 4, 5

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 *