Python Program to Convert String to List
list(your_string), which splits the string into a list of its characters.Examples
How to Think About It
list() breaks the string into characters, while split() breaks it into words. Here, we focus on converting to a list of characters.Algorithm
Code
input_string = "hello" result_list = list(input_string) print(result_list)
Dry Run
Let's trace the string "hello" through the code.
Input string
input_string = "hello"
Convert to list
result_list = list(input_string) # ['h', 'e', 'l', 'l', 'o']
Print result
print(result_list) # Output: ['h', 'e', 'l', 'l', 'o']
| Step | Value of result_list |
|---|---|
| After conversion | ['h', 'e', 'l', 'l', 'o'] |
Why This Works
Step 1: Using list() function
The list() function takes any iterable, like a string, and creates a list of its elements, which are characters in this case.
Step 2: String as iterable
A string is a sequence of characters, so when passed to list(), it splits into individual characters.
Step 3: Resulting list
The output is a list where each element is one character from the original string, preserving order.
Alternative Approaches
input_string = "hello world" result_list = input_string.split() print(result_list)
input_string = "hello" result_list = [char for char in input_string] print(result_list)
Complexity: O(n) time, O(n) space
Time Complexity
The list() function iterates over each character in the string once, so the time grows linearly with the string length.
Space Complexity
A new list is created with one element per character, so space used is proportional to the string length.
Which Approach is Fastest?
Using list() is the fastest and simplest for character lists; split() is best for word lists but slightly slower due to parsing.
| Approach | Time | Space | Best For |
|---|---|---|---|
| list() | O(n) | O(n) | List of characters |
| split() | O(n) | O(n) | List of words |
| List comprehension | O(n) | O(n) | Custom character processing |
list(your_string) to quickly get a list of characters from a string.list() or split() often results in just the original string, not a list.