5 Ways to Remove Brackets from List in Python

What is Python List

Python list is used to hold the sequence of different types of data. Python lists are changeable, which means we can change their elements after they’ve been formed. However, Python has six data types that may be used to store sequences, with the list being the most common and efficient.

Definition: A list may be described as a collection of different types of values or objects. The comma “,” separates the elements in the list, which are contained in square brackets [].

5 Ways to Remove Brackets from List in Python

  1. join method
  2. for loop
  3. translate method
  4. string slicing method
  5. Using * operator with separator method

Method 01: Using Join Function

Python has a built-in function called join(). This approach extracts all items from a sequence. The program then puts all of the pieces together and prints them as a single element. For the join function, we can only use characters or strings.

Syntax:

string.join(sequence)

Parameter:

  • sequence: A sequence may be a list or tuple.

Return: A combined element

Example 01: join function without using loop

inp_list=['1','2','3','4','a']
print("Original list",inp_list)
out_list=(','.join(inp_list))
print("After removing bracket",out_list)

Output:

Original list ['1', '2', '3', '4', 'a']
After removing bracket 1,2,3,4,a

Example 02: join function using loop

inp_list=["1","2","3","4","5","a"]
print("Original list",inp_list)
out_list=(','.join(str(a)for a in inp_list))
print("After removing bracket",out_list)

Output:

Original list ['1', '2', '3', '4', '5', 'a']
After removing bracket 1,2,3,4,5,a

Method 02: Using the for Loop

Example 03:

inp_list=['1','2','3','4','5','a']
print("Original list:",inp_list)
print("After removing bracket:")
for out_list in inp_list: 
      print(out_list,end=',')
print(" ")

Output:

Original list: ['1', '2', '3', '4', '5', 'a']
After removing bracket:
1,2,3,4,5,a, 

Method 03: Using the Translate method

The translate function returns a string in which each character is mapped to its translation table equivalent. In Python, this approach has restrictions when printing lists without brackets or quotes. The given characters are substitute with the stated characters in the translation process.

Example 04:

inp_list = ["1","2","3","4","5","a"] 
target = {39:None, 91:None , 93:None} 
ans=(str(inp_list).translate(target))
print("Original list:",inp_list)
print("After removing brackets:",ans)

Output:

Original list: ['1', '2', '3', '4', '5', 'a']
After removing brackets: 1, 2, 3, 4, 5, a

Method 04: String Slicing method

String slicing is the process of producing a substring from a set of values. It has a start element, an end element, and an increment value.

Syntax:

[start:stop:step]

Parameters:

  • start: start element
  • stop: end element
  • step: increment value

Return: substring

Example 05:

inp_list =['1','2','3','4','5','a']
out_list = str(inp_list)[1:-1] 
print("Original list:",inp_list)
print("After removing brackets:",out_list)

Output:

Original list: ['1', '2', '3', '4', '5', 'a']
After removing brackets: '1', '2', '3', '4', '5', 'a'

Method 05: Using the * operator with the Separator method

* Operator unpacks each item of the ordered list within one command without indexing each item of the list one by one.

Example 06:

inp_list =["1",'2','3','4','5','a']
print("Original list:",inp_list)
print("After removing brackets:",*inp_list, sep = ',')

Output:

Original list: ['1', '2', '3', '4', '5', 'a']
After removing brackets:,1,2,3,4,5,a

FAQs

How to Remove an element from a Python list

There are two methods by which you can remove an element from the python list

  1. Using remove() method
  2. Using pop() method

How to add Elements to the Python list

There are three methods by which you can add an element to the python list

  1. append() method
  2. insert() method
  3. extend() method

Conclusion

That’s all for this article, if you have any confusion contact us through our website or email us at [email protected] or by using LinkedIn

Suggested Articles:

  1. Python List

Leave a Comment