Python How to Convert String to List Easily
You can convert a string to a list in Python by using
list(your_string) to get a list of characters or your_string.split() to split the string into words.Examples
Input"hello"
Output['h', 'e', 'l', 'l', 'o']
Input"apple orange banana"
Output['apple', 'orange', 'banana']
Input""
Output[]
How to Think About It
To convert a string to a list, first decide if you want each character separately or words separated by spaces. Use
list() to get characters or split() to get words as list items.Algorithm
1
Get the input string.2
If you want each character as a list item, apply <code>list()</code> to the string.3
If you want words separated by spaces, apply <code>split()</code> to the string.4
Return the resulting list.Code
python
text = "hello world" list_chars = list(text) list_words = text.split() print(list_chars) print(list_words)
Output
['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']
['hello', 'world']
Dry Run
Let's trace converting 'hello world' to a list of characters and words.
1
Convert to list of characters
list_chars = list('hello world') results in ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']
2
Convert to list of words
list_words = 'hello world'.split() results in ['hello', 'world']
| Operation | Result |
|---|---|
| list('hello world') | ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'] |
| 'hello world'.split() | ['hello', 'world'] |
Why This Works
Step 1: Using list()
The list() function takes the string and creates a list where each character, including spaces, becomes an item.
Step 2: Using split()
The split() method breaks the string into parts separated by spaces and returns a list of these words.
Alternative Approaches
Using list comprehension
python
text = "hello" list_chars = [char for char in text] print(list_chars)
This creates a list of characters manually; useful if you want to add conditions or transformations.
Using split with custom separator
python
text = "apple,orange,banana" list_items = text.split(",") print(list_items)
Splits the string by commas instead of spaces, useful for CSV-like strings.
Complexity: O(n) time, O(n) space
Time Complexity
Both list() and split() iterate over the string once, so time grows linearly with string length.
Space Complexity
Both methods create a new list storing all characters or words, so space used is proportional to the string size.
Which Approach is Fastest?
list() and split() are both efficient; choose based on whether you want characters or words.
| Approach | Time | Space | Best For |
|---|---|---|---|
| list() | O(n) | O(n) | List of characters |
| split() | O(n) | O(n) | List of words separated by spaces |
| List comprehension | O(n) | O(n) | Custom character processing |
| split() with separator | O(n) | O(n) | Splitting by custom delimiter |
Use
list() for characters and split() for words to convert strings to lists easily.Trying to use
list() when you want words instead of characters, which results in a list of single letters.