In Python, a list corresponds to a collection of data objects that can be of different types, unlike the array which can only hold objects of the same type. There is no doubt that lists are one of the most effective and useful features in Python. This tutorial explains the approaches to printing a list in Python without brackets using various methods.
How to Print Python List Without Brackets?
The following approaches/methods can be used in Python to print a list without brackets:
- Using the “for” Loop.
- Using the “join()”.
- Using the “List Comprehension”.
- Using the “str()” Function.
- Using the “asterisk *” Operator.
Method 1: Print List Without Brackets in Python Using the “for” Loop
The “for” loop is used to iterate over/through the list or any iterable object in Python. The following example code uses this loop to print the list without brackets.
Example
Let’s overview the example code:
for ele in list_value:
print(ele, end=",")
In the above example code, the “for” loop iterates through the defined list and prints all the elements of the list without brackets.
Note: The “end=” is used to specify the separator between the list element such as in the above case the “comma (,)” is used between the list.
Output
The list without square brackets has been displayed in the above output.
Method 2: Print List Without Brackets in Python Using the “join()” Method
The “join()” method accepts all elements/items in an iterable and joins/appends them into one particular string. However, a string must be defined as the separator. This “join()” method can also be utilized to show the bracket-free list.
Syntax
Example
Here is an example code:
print(', '.join(list_value))
In the above code, the “list_value” item is printed on the console screen using the “join()” method. This method merges all the elements of the string into an empty string with a comma separator (,).
Output
In the above output, the unbracketed list has been displayed.
Note: The method works only with a list of strings; it does not work with a list of integers or floats.
Method 3: Print List Without Brackets in Python Using “List Comprehension”
The “List Comprehension” is a convenient way of creating a new list by iterating over a sequence, such as a list or a string. This is used with the “for” loop to display a list without brackets.
Example
Here is a code that uses list comprehension to print a list without brackets:
[print(i, end=' ') for i in list_value]
In the above code, the “List Comprehension” is utilized with the “for” loop to iterate over the list and returns the list element without brackets.
Output
Method 4: Print List Without Brackets in Python Utilizing the “str()” Function
The “str()” function is used along with string-slicing techniques to print the given list without brackets in Python.
Example
The following code illustrates the discussed concept:
print(str(list_value)[1:-1])
In the above code lines, the “string slicing” technique is used to remove the list brackets by removing the first and last string characters.
Output
Method 5: Print List Without Brackets in Python Utilizing the “asterisk” ‘*’ Operator
The “asterisk *” operator is utilized to unpack the elements/items in the list and assign them to the variables. It can also be utilized to display the list elements without brackets, by passing the list as an argument to the print function.
Example
The below code is used to print the list without brackets:
print(*list_value, sep = ' ') #without separator
print(*list_value, sep = ', ') #without separator
In this code, the “asterisk *” operator is used to display the list elements without brackets using the empty and comma separators, respectively.
Output
As analyzed, the list without brackets has been displayed appropriately.
Conclusion
To print the list without brackets in Python, apply the “for” loop, “join()” method, “List Comprehension”, “str()” function, or the “asterisk” ‘*’ operators. The simplest approach is the “for” loop that is used to iterate through the list and print the list without brackets. The other methods can also print the list without brackets using any specified separator verify efficiently in Python.