Saturday, November 16, 2024
Google search engine
HomeLanguagesPython | Remove square brackets from list

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 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.

RELATED ARTICLES

Most Popular

Recent Comments