The Wayback Machine - https://web.archive.org/web/20240622150248/https://www.geeksforgeeks.org/python-remove-square-brackets-from-list/
Open In App

Python | Remove square brackets from list

Last Updated : 13 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 the 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 1: Using str() + list slicing 

The shorthand that can be applied, without having the need to access each element of list is to convert the entire list to a 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. 

Python3




# 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)


Output : 

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

Time Complexity: O(n), where n is the length of the list test_list 
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list 

Method #2 : Using str() and replace() methods

Python3




# Python3 code to demonstrate working of
# Remove square brackets from list
 
# 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
x=str(test_list)
x=x.replace("[","")
x=x.replace("]","")
# printing result
print("List after removing square brackets : " + x)


Output

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

Method 3: Using map() and join() methods

Step-by-step approach:

  • Initialize the list.
  • Convert each element of the list to a string using the map() method.
  • Join the list of strings using the join() method and assign the result to a variable.
  • Print the result.

Below is the implementation of the above approach:

Python3




# Python3 code to demonstrate working of
# Remove square brackets from list
# using map() and join() methods
 
# 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 map() and join() methods
res = ', '.join(map(str, test_list))
 
# printing result
print("List after removing square brackets : " + res)


Output

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

Time complexity: O(n), where n is the length of the list.
Auxiliary space: O(n), for the intermediate list of strings created using the map() method.

Method #4: Using a loop to concatenate elements to a string

Step-by-step approach:

  • Initialize the list test_list with the values.
  • Print the original list using print(“The original list is : ” + str(test_list)).
  • Create an empty string res to hold the final concatenated string.
  • Use a loop to iterate over each element of the list. For each element:
    • Convert the element to a string using str(test_list[i]).
    • If it’s the first element, add it to res without a comma or space. Otherwise, add a comma and a space before adding the element to res.
  • Print the final concatenated string using print(“List after removing square brackets : ” + res).

Below is the implementation of the above approach:

Python3




# Python3 code to demonstrate working of
# Remove square brackets from list
# using a loop to concatenate elements to a string
 
# 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 a loop to concatenate elements to a string
res = ""
for i in range(len(test_list)):
    if i == 0:
        res += str(test_list[i])
    else:
        res += ", " + str(test_list[i])
 
# printing result
print("List after removing square brackets : " + res)


Output

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

Time complexity: O(n), where n is the length of the list. The loop iterates once over each element of the list, so the time complexity is linear with respect to the length of the list.
Auxiliary space: O(n), where n is the length of the list. The space used by the string res is proportional to the length of the list, since each element is converted to a string and concatenated with a comma and a space.



Similar Reads

How to remove text inside brackets in Python?
In this article, we will learn how to remove content inside brackets without removing brackets in python. Examples: Input: (hai)geeks Output: ()geeks Input: (geeks)for(geeks) Output: ()for() We can remove content inside brackets without removing brackets in 2 methods, one of them is to use the inbuilt methods from the re library and the second meth
4 min read
How to remove brackets from text file in Python ?
Sometimes it becomes tough to remove brackets from the text file which is unnecessary to us. Hence, python can do this for us. In python, we can remove brackets with the help of regular expressions. Syntax: # import re module for using regular expression import re patn = re.sub(pattern, repl, sentence) # pattern is the special RE expression for fin
3 min read
Python | Extract Numbers in Brackets in String
Sometimes, while working with Python strings, we can have a problem in which we have to perform the task of extracting numbers in strings that are enclosed in brackets. Let's discuss the certain ways in which this task can be performed. Method 1: Using regex The way to solve this task is to construct a regex string that can return all the numbers i
6 min read
Python | Extract substrings between brackets
Sometimes, while working with Python strings, we can have a problem in which we need to extract the substrings between certain characters that can be bracketed. This can have application in cases we have tuples embedded in string. Let's discuss certain ways in which this task can be performed. Method #1: Using regex One way to solve this problem is
6 min read
Python - Matching Pairs of Brackets
Sometimes, while working with Python data, we can have a problem in which we need to pair all the elements with the suitable closing brackets, and can have data associated with pairing. This kind of problem is classic application of Stack Data Structure and can have usage across many domains. Let's discuss a certain way in which this task can be do
4 min read
Python Program to Remove First Diagonal Elements from a Square Matrix
Given a square matrix of N*N dimension, the task is to write a Python program to remove the first diagonal. Examples: Input : test_list = [[5, 3, 3, 2, 1], [5, 6, 7, 8, 2], [9, 3, 4, 6, 7], [0, 1, 2, 3, 5], [2, 5, 4, 3, 5]] Output : [[3, 3, 2, 1], [5, 7, 8, 2], [9, 3, 6, 7], [0, 1, 2, 5], [2, 5, 4, 3]] Explanation : Removed 5, 6, 4, 3, 5 from lists
6 min read
Python Program to Square Each Odd Number in a List using List Comprehension
Given a list, the task is to write a Python Program to square each odd number in a list using list comprehension. Square Each Odd Number in a List using List Comprehension List comprehensions are used for creating new lists from other iterables like tuples, strings, arrays, lists, etc. A list comprehension consists of brackets containing the expres
3 min read
Python | Remove all values from a list present in other list
Sometimes we need to perform the operation of removing all the items from the lists that are present in another list, i.e we are given some of the invalid numbers in one list which need to be get ridden from the original list. Let's discuss various ways How to remove the elements of a list from another list in Python. Illustration: Input: List one
10 min read
Python - Remove empty List from List
Sometimes, while working with python, we can have a problem in which we need to filter out certain empty data. These can be none, empty string, etc. This can have applications in many domains. Let us discuss certain ways in which the removal of empty lists can be performed. Method 1: Using list comprehension: This is one of the ways in which this p
8 min read
Python Program to Check if a Number is Perfect Square in List
In this article, we will understand how to print all perfect squares from a list in Python using the list comprehension and math module. The main function that is present in the math module that we will be using is the sqrt() and floor() function. Let's see the output of these two functions one by one. Check if a Number is Perfect Square Using Squa
3 min read
Article Tags :
Practice Tags :