Ctrl K

Cut on the Separator

Easy
You are given a list of strings chunks and a separator character sep. Cut every string at each occurrence of sep (the separator itself goes into no piece) and throw away empty pieces. Return all the non-empty pieces as one list: first the pieces of the first string from left to right, then those of the second, and so on.
Example 1
chunks=['red,green', 'blue', ',,white,'], sep=',' → ['red', 'green', 'blue', 'white']

"red,green" gives "red" and "green", "blue" stays as it is, and ",,white," leaves only "white" once the empty pieces are dropped.

Example 2
chunks=['##', '#'], sep='#' → []

Every piece is empty, so the answer is an empty list.

Example 3
chunks=['a.b', 'c|d'], sep='|' → ['a.b', 'c', 'd']
Constraints
  • 1 ≤ len(chunks) ≤ 100
  • 1 ≤ len(chunks[i]) ≤ 20
  • chunks[i] consists of lowercase English letters, digits and characters from .,|$#@-
  • sep is one character from .,|$#@-
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsTwo Words, Closest Apart EasyOne-Row Typing EasyDinner for Two EasyPhrases with Synonyms EasyWord from a License Plate Easy
loading Python…
Input
chunks=['red,green', 'blue', ',,white,'], sep=','
Expected
['red', 'green', 'blue', 'white']

Run checks the first 3 tests in your browser. Submit checks all 108 tests on the server, hidden ones included.