Remove square brackets from a list in Python
You need to familiar with only one thing to remove square braces from a list. (i.e; .replace() function)
Removing braces from a list can be mind boggling, you can find difficulty while removing braces as you use list to store various characters of different data types, but you want to show your elements without without brackets and inverted commas. So be Patience and read the full article :--
let us know first what does .replace() function do.
The word itself says what it does.
Let me explain you, the work of this function is to replace whatever is already there with something else or whatever you want at that place. This function is only used with string.
Don't worry brother you can understand from this example :-
1. str1= "EDUshark"
(when you write print( str1 ) you get output as "EDUshark" itself. But I want EDUshark without inverted commas.)
write this command :-
str1.replace(' " ', ' ')
print(str1)
output:-
EDUshark
Here what I does is, I simply replaced inverted commas ("") by a space, And let me tell you a fact that spaces are invisible.
So, now lets do the same with lists but here you have to first convert the list into a string lets crack it :-
list1=['DevilzSpace','EDUshark',17,2]
list2=str(list1).replace("'"," ")
print(list2)
output:-
[DevilzSpace, EDUshark,17,2]
(still brackets are their, then add one more syntax to it)
list2=str(list1).replace("'"," ").replace("[","").replace("]","")
print(list2)
output:-
DevilzSpace, EDUshark,17,2
See the magic happened.
Thank you.
Please Comment your suggestion .
Comments
Post a Comment