Python | Remove square brackets from list

Sometimes, while working with displaying the contents of list, the square brackets, both opening and closing are undesired. For this when we need to print whole list without accessing the elements for loops, we require a method to perform this. Let’s discuss a shorthand by which this task can be performed.

Method : Using str() + list slicing
The shorthand that can be applied, without having need to access each element of list is to convert the entire list to string and then strip the initial and last character of list using list slicing. This won’t work if list contains a string. In that case, each element can be joined using join(), as discussed in many other articles.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Remove square brackets from list
# using str() + list slicing
  
# initialize list
test_list = [5, 6, 8, 9, 10, 21]
  
# printing original list
print("The original list is : " + str(test_list))
  
# Remove square brackets from list
# using str() + list slicing
res = str(test_list)[1:-1]
  
# printing result
print("List after removing square brackets : " + res)

chevron_right


Output :

The original list is : [5, 6, 8, 9, 10, 21]
List after removing square brackets : 5, 6, 8, 9, 10, 21


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.