5 Ways to Remove Brackets from List in Python

We all know What is a list in python? Whenever we hear a word list. We will say it is a collection of elements separated by commas and enclosed within the square bracket. Have you ever thought that is there any possible ways to remove brackets from a list? I guess many of you are thinking How to do it? If you don’t know about it. Just read this article till the end, and this will teach you. Let us see 5 different ways to remove brackets from a list.

We know that list is a collection of elements separated by commas and enclosed within square brackets. A list of elements may be an integer, string, or character. This is the basic thing we have to know about a list. Now let us move on to how to remove brackets from the list in python. We can remove brackets using five different ways. We will learn about every possible way in a detailed manner.

What are the possible ways to remove brackets from a list?

1. Using join function to remove brackets from a list in Python

join() is a built-in function in python. This method takes all the elements from a given sequence. Then it joins all the elements and prints them into a single element. We can use only characters or strings for the join function. 

Syntax

string.join(sequence)

Parameter

sequence: A sequence may be a list or tuple.

Returns

A combined element

Example 1: join function without using loop

lst=['a','b','c']
print("Original list",lst)
new_lst=(','.join(lst))
print("After removing bracket",new_lst)

Declaring a list of elements to the variable. Using join function to join all the elements and using commas to separate the elements.

Output

Original list ['a', 'b', 'c']
After removing bracket a,b,c

Example 2: join function using loop

lst=["Python","Pool","Latracal"]
print("Original list",lst)
new_lst=(','.join(str(a)for a in lst))
print("After removing bracket",new_lst)

Declaring a list of elements to the variable. Using join function to join all the elements and using commas to separate the variables. for loop to remove brackets.

Output

Original list ['Python', 'Pool', 'Latracal']
After removing bracket Python,Pool,Latracal

2.Using for loop to remove brackets from a list in python

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

Declaring elements of the list. Using for loop to iterate till the end of the loop. Each iteration will take an element and store it in a variable named new_lst. Printing the elements without bracket.

Output

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

3. Using translate method to remove brackets from a list in Python

The translate method returns a string where each character is mapped to its corresponding character in the translation table.

Syntax

string.join(iterable)

Parameter

iterable: Any iterable object where all the returned values are strings

Example

lst = ["Python","Pool","Latracal"] 
target = {39:None, 91:None , 93:None} 
a=(str(lst).translate(target))
print("Original list:",lst)
print("After removing brackets:",a)

Output

Original list: ['Python', 'Pool', 'Latracal']
After removing brackets: Python, Pool, Latracal

4. Using string slicing method to remove brackets from a list in Python

String slicing is creating a substring from a given range of values. It contains the start element, end element, increment value.

Syntax

[start:stop:step]

Parameters

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

Returns

substring

Example

lst =[1,2,3,4,5]
new_lst = str(lst)[1:-1] 
print("Original list:",lst)
print("After removing brackets:",new_lst)

Declaring a list of elements. Using string slicing. We need to remove brackets only, so we are giving as [1:-1].

Output

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

5. Using separator to remove brackets from a list in Python

lst = [1,'a',"Python"]
print(*lst, sep = ',')

Output

1,a,Python

Bonus: How to get a list of elements as the input

l=[]
n=int(input("Enter no of elements:"))
for i in range(n):
    element=int(input("Enter list element:"))
    l.append(element)
print("The list elements are:",l)

In this article, we have done every program with a list. So it is necessary to know how to get a list element as input. Try to implement all the programs by getting input from the user.

Output

Enter no of elements:5
Enter list element:6
Enter list element:2
Enter list element:4
Enter list element:6
Enter list element:2
The list elements are: [6, 2, 4, 6, 2]
1. Is it possible to give an integer in a join function?

No, it is not possible. A join() function only accepts a character or string.

Conclusion

So far, we have learned how to remove brackets from a list in python. Try to implement a program by getting the input from the user. It is a good practice. Implement all the programs on your own. In case of any doubts, feel free to ask in the comment section. We will always help you.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments