0
0
PythonProgramBeginner · 2 min read

Python Program to Convert String to List

You can convert a string to a list in Python using list(your_string), which splits the string into a list of its characters.
📋

Examples

Input"hello"
Output['h', 'e', 'l', 'l', 'o']
Input"12345"
Output['1', '2', '3', '4', '5']
Input""
Output[]
🧠

How to Think About It

To convert a string to a list, think about what you want in the list: each character separately or words separated by spaces. Using list() breaks the string into characters, while split() breaks it into words. Here, we focus on converting to a list of characters.
📐

Algorithm

1
Take the input string.
2
Use the <code>list()</code> function to convert the string into a list of characters.
3
Return or print the resulting list.
💻

Code

python
input_string = "hello"
result_list = list(input_string)
print(result_list)
Output
['h', 'e', 'l', 'l', 'o']
🔍

Dry Run

Let's trace the string "hello" through the code.

1

Input string

input_string = "hello"

2

Convert to list

result_list = list(input_string) # ['h', 'e', 'l', 'l', 'o']

3

Print result

print(result_list) # Output: ['h', 'e', 'l', 'l', 'o']

StepValue 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

Using split() to convert string to list of words
python
input_string = "hello world"
result_list = input_string.split()
print(result_list)
This splits the string into words instead of characters, useful when you want a list of words.
Using list comprehension for custom processing
python
input_string = "hello"
result_list = [char for char in input_string]
print(result_list)
This method is more flexible if you want to filter or modify characters during conversion.

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.

ApproachTimeSpaceBest For
list()O(n)O(n)List of characters
split()O(n)O(n)List of words
List comprehensionO(n)O(n)Custom character processing
💡
Use list(your_string) to quickly get a list of characters from a string.
⚠️
Trying to convert a string to a list without using list() or split() often results in just the original string, not a list.