Python: Separate Parentheses Groups Perfectly
Split Matched Parentheses Groups
Given a string consisting of whitespace and groups of matched parentheses, write a Python program to split it into groups of perfectly matched parentheses without any whitespace.
Input: ( ()) ((()()())) (()) () Output: ['(())', '((()()()))', '(())', '()'] Input: () (( ( )() ( )) ) ( ()) Output: ['()', '((()()()))', '(())']
Visual Presentation:


Sample Solution:
Python Code:
Sample Output:
Parentheses string: ( ()) ((()()())) (()) () Separate parentheses groups of the said string: ['(())', '((()()()))', '(())', '()'] Parentheses string: () (( ( )() ( )) ) ( ()) Separate parentheses groups of the said string: ['()', '((()()()))', '(())']
Flowchart:

For more Practice: Solve these Related Problems:
- Write a Python program to extract groups of perfectly matched parentheses from a string using a stack-based approach.
- Write a Python program to parse a string containing nested parentheses and return a list of top-level balanced groups.
- Write a Python program to split a string by whitespace and extract only the groups that form balanced parentheses using regular expressions.
- Write a Python program to iterate over a string and output all contiguous substrings that are valid groups of matched parentheses.
Python Code Editor :
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: List integers containing exactly three distinct values, such that no integer repeats twice consecutively.
Next: Find the indexes of numbers, below a given threshold.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics