Whenever we print list in Python, we generally use str(list) because of which we have single quotes in the output list. Suppose if the problem requires to print solution without quotes. Let’s see some ways to print lists without parentheses and quotes.
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:
[‘Fuji’, ‘McIntosh’, ‘Red Delicious’, ‘Gala’, ‘Jonagold’]
But what if you want to print lists without parenthesis and commas? Well, in this article we’re going to discuss different methods to do so.
Table of Contents
1). Using for loop:
It 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
Fuji, McIntosh, Red Delicious, Gala, Jonagold
- First, we declare the list.
- Using for loop, we iterate through each value of a list and print that value with a comma and space. (if you only want to add a space between the values of a string then just add a space in the end parameter and skip the next steps)
- The for loop also added a comma and space at last so to remove this we move over the cursor to the last two printed items.
- Furthermore, at the last step, we move the comma with space.
2). Using join() method:
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
Fuji, McIntosh, Red Delicious, Gala, Jonagold
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
TypeError: sequence item 0: expected str instance, int found
But, if you want to use the join function to print lists 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
2, 4, 6, 8
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.
3). Using sep keyword in print:
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
Fuji, McIntosh, Red Delicious, Gala, Jonagold
* 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.
4). Using translate() method:
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
Fuji, McIntosh, Red Delicious, Gala, Jonagold
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.
Conclusion:
There could be different scenarios when you want to print lists without parentheses in python. In this article, we’ve discussed four distinct methods using different python functions.