Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Python | Split String Empty Separator
1
Python | Split String Empty Separator

Rate this post

Summary: You can split a string using an empty separator using –
(i) list constructor
(ii) map+lambda
(iii) regex
(iv) list comprehension


[b]Minimal Example:[/b]

Code:
text = '12345' # Using list() print(list(text)) # Using map+lambda print(list(map(lambda c: c, text))) # Using list comprehension print([x for x in text]) # Using regex import re # Approach 1 print([x for x in re.split('', text) if x != '']) # Approach 2 print(re.findall('.', text))

Problem Formulation

[Image: 1f4dc.png]Problem: How to split a string using an empty string as a separator?

Example: Consider the following snippet –

Code:
a = 'abcd' print(a.split(''))

Output:

Code:
Traceback (most recent call last): File "C:\Users\SHUBHAM SAYON\PycharmProjects\Finxter\Blogs\Finxter.py", line 2, in <module> a.split('') ValueError: empty separator

Expected Output:

Code:
['a', 'b', 'c', 'd']

So, this essentially means that when you try to split a string by using an empty string as the separator, you will get a ValueError. Thus, your task is to find out how to eliminate this error and split the string in a way such that each character of the string is separately stored as an item in a list.

Now that we have a clear picture of the problem let us dive into the solutions to solve the problem.

Method 1: Use list()

Approach: Use the list() constructor and pass the given string as an argument within it as the input, which will split the string into separate characters.

Note:
Code:
list()
 creates a new list object that contains items obtained by iterating over the input iterable. Since a string is an iterable formed by combining a group of characters, hence, iterating over it using the list constructor yields a single character at each iteration which represents individual items in the newly formed list.

Code:

Code:
a = 'abcd' print(list(a)) # ['a', 'b', 'c', 'd']

[Image: 1f30e.png]Related Read: Python list() — A Simple Guide with Video

Method 2: Use map() and lambda

Approach: Use the 
Code:
map()
 to execute a certain lambda function on the given string. All you need to do is to create a lambda function that simply returns the character passed to it as the input to the map object. That’s it! However, the map method will return a map object, so you must convert it to a list using the 
Code:
list()
 function.

Code:

Code:
a = 'abcd' print(list(map(lambda c: c, a))) # ['a', 'b', 'c', 'd']
[b]Method 3: Use a list comprehension[/b]

Approach: Use a list comprehension that returns a new list containing each character of the given string as individual items.

Code:

Code:
a = 'abcd' print([x for x in a]) # ['a', 'b', 'c', 'd']

[Image: 1f30e.png]Related Read: List Comprehension in Python — A Helpful Illustrated Guide

Method 4: Using regex

The 
Code:
re.findall(pattern, string)
 method scans 
Code:
string
 from left to right, searching for all non-overlapping matches of the 
Code:
pattern
. It returns a list of strings in the matching order when scanning the string from left to right.

[Image: 1f30e.png]Related Read: Python re.findall() – Everything You Need to Know

Approach: Use the regular expression
Code:
re.findall('.',a)
that finds all characters in the given string ‘
Code:
a
‘ and stires them in a list as individual items.

Code:

Code:
import re a = 'abcd' print(re.findall('.',a)) # ['a', 'b', 'c', 'd']

Alternatively, you can also use the split method of the regex library in a list comprehension which returns each character of the string and eliminates empty strings.

Code:

Code:
import re a = 'abcd' print([x for x in re.split('',a) if x!='']) # ['a', 'b', 'c', 'd']

[Image: 1f30e.png]Related Read: Python Regex Split

Do you want to master the regex superpower? Check out my new book The Smartest Way to Learn Regular Expressions in Python with the innovative 3-step approach for active learning: (1) study a book chapter, (2) solve a code puzzle, and (3) watch an educational chapter video.

Conclusion

Hurrah! We have successfully solved the given problem using as many as four (five, to be honest) different ways. I hope this article helped you and answered your queries. Please subscribe and stay tuned for more interesting articles and solutions in the future.

Happy coding! [Image: 1f642.png]

Regex Humor

[Image: image-133.png][i]Wait, forgot to escape a space. Wheeeeee[taptaptap]eeeeee. (source)[/i]
Reply

Users browsing this thread:
1 Guest(s)


Forum Jump: