list("given string") to extract each character of the given string and store them as individual items in a list.
# OUTPUT: [‘a’, ‘b’, ‘c’]
Problem: Given a string; How will you split the string into a list of characters?
Example: Let’s visualize the problem with the help of an example:
|input = “finxter”|
output = [‘f’, ‘i’, ‘n’, ‘x’, ‘t’, ‘e’, ‘r’]
Now that we have an overview of our problem let us dive into the solutions without further ado.
Method 1: Using The list Constructor
Approach: One of the simplest ways to solve the given problem is to use the list constructor and pass the given string into it as the input.
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.
text = "finxter" print(list(text)) # ['f', 'i', 'n', 'x', 't', 'e', 'r']
💎Related Tutorial: Python list() — A Simple Guide with Video
Method 2: Using a List Comprehension
Another way to split the given string into characters would be to use a list comprehension such that the list comprehension returns a new list containing each character of the given string as individual items.
text = "finxter" print([x for x in text]) # ['f', 'i', 'n', 'x', 't', 'e', 'r']
Prerequisite: To understand what happened in the above code, it is essential to know what a list comprehension does. In simple words, a list comprehension in Python is a compact way of creating lists. The simple formula is
[expression + context], where the “expression” determines what to do with each list element. And the “context” determines what elements to select. The context can consist of an arbitrary number of
if statements. To learn more about list comprehensions, head on to this detailed guide on list comprehensions.
Explanation: Well! Now that you know what list comprehensions are, let’s try to understand what the above code does. In our solution, the context variable
x is used to extract each character from the given string by iterating across each character of the string one by one with the help of a
for loop. This context variable
x also happens to be the expression of our list comprehension as it stores the individual characters of the given string as separate items in the newly formed list.
Multi-line Solution: Another approach to formulating the above solution is to use a for loop. The idea is pretty similar; however, we will not be using a list comprehension in this case. Instead, we will use a for loop to iterate across individual characters of the given string and store them one by one in a new list with the help of the append method.
text = "finxter" res =  for i in text: res.append(i) print(res) # ['f', 'i', 'n', 'x', 't', 'e', 'r']
Yet another way of solving the given problem is to use a lambda function within the
map function. Now, this is complex and certainly not the best fit solution to the given problem. However, it may (or may not ;P) be appropriate when you are handling really complex tasks. So, here’s how to use the two built-in Python functions to solve the given problem:
text = "finxter" print(list(map(lambda c: c, text))) # ['f', 'i', 'n', 'x', 't', 'e', 'r']
map() function is used to execute a specified function for each item of an iterable. In this case, the iterable is the given string and each character of the string represents an individual item within it. Now, all we need to do is to create a lambda function that simply returns the character passed to it as the input. That’s it! However, the map method will return a map object, so you must convert it to a list using the
list() function. Silly! Isn’t it? Nevertheless, it works!
Hurrah! We have successfully solved the given problem using as many as three different ways. I hope you enjoyed this article and it helps you in your Python coding journey. Please subscribe and stay tuned for more interesting articles!