Last Updated On By Khizer Ali
In this article, we will figure out four unique methods to print list without brackets and quotes in Python but foremost, what is list?
A list is the data structure in python that allows you to store a collection of items stored in a variable. Lists are mutable or changeable. We can define a list with the following syntax.
apples = ["Fuji", "McIntosh", "Red Delicious", "Gala", "Jonagold"]
print(apples)
The above line defines the list of different types of apples stored in the “apples” variable. It will give us the output like this:
But what if you want to display it by removing brackets and commas from list? Well, in this article we’re going to discuss different methods to do so.
Table of Contents
For Loop is the most general solution that comes first to the mind when you try to solve this problem. In this, we iterate through each value of a list and print that value.
apples = ["Fuji", "McIntosh", "Red Delicious", "Gala", "Jonagold"]
for apple in apples:
print(apple, end=", ")
print("\b\b", end="")
print(" ")
Output:
There is a built-in method in python called join(). It takes an iterable and converts it into a string with the specified value given with it.
apples = ["Fuji", "McIntosh", "Red Delicious", "Gala", "Jonagold"]
separator = ", "
print(separator.join(apples))
Output:
You can define that value in the separator variable through which you want to join the items in the list.
One thing to remember that if you want to join the integer values, then the join method will not work because it is a string method. It will generate the following error.
Even_no = [2, 4, 6, 8]
separator = ", "
print(separator.join(Even_no))
Output:
But, if you want to use join function for print list without brackets and commas, then you can accomplish this by using map() function.
Even_no = [2, 4, 6, 8]
separator = ", "
print(separator.join(map(str, Even_no)))
Output:
The map() function takes two argument, first is a function and second is a item from iterable which need to be mapped and it passes to that function which is mention is the first argument.
The map() function returns an iterable which is now converted into a string and then it passes to the join() method.
Separator, written as “sep” is a keyword which is used in a print statement when you want to separate two different values or objects in a print statement with a specific string.
apples = ["Fuji", "McIntosh", "Red Delicious", "Gala", "Jonagold"]
print(*apples, sep=", ")
Output:
* written in front of apples causes apples to be unpacked in items and converted into a string, then these items are combined with value which is specified in the sep keyword.
If you just want to separate the items and print a list without the brackets and single quotes, then you don’t need to specify the value of sep because it has a default value of whitespace.
The translate() is a string method which will change the string by replacing or deleting the characters.
apples = ["Fuji", "McIntosh", "Red Delicious", "Gala", "Jonagold"]
target = {39 : None, 91 : None, 93 : None}
print(str(apples).translate(target))
Output:
We specific the changes which we want in our string in the “target” variable, where 39, 91, and 93 are the ASCII codes for “,” , “[“ and “]” respectively. We set these characters to none and translate our list by converting it into a string because translate is a string method.
There could be uncommon scenarios when you want to print list without brackets and quotes in Python. In this article, we’ve discussed four distinct methods using different python functions.