Split list by character python

❮ String Methods


Example

Split a string into a list where each word is a list item:

txt = "welcome to the jungle"

x = txt.split()

print(x)

Try it Yourself »


Definition and Usage

The split() method splits a string into a list.

You can specify the separator, default separator is any whitespace.

Note: When maxsplit is specified, the list will contain the specified number of elements plus one.


Syntax

string.split(separator, maxsplit)

Parameter Values

ParameterDescription
separator Optional. Specifies the separator to use when splitting the string. By default any whitespace is a separator
maxsplit Optional. Specifies how many splits to do. Default value is -1, which is "all occurrences"

More Examples

Example

Split the string, using comma, followed by a space, as a separator:

txt = "hello, my name is Peter, I am 26 years old"

x = txt.split(", ")

print(x)

Try it Yourself »

Example

Use a hash character as a separator:

txt = "apple#banana#cherry#orange"

x = txt.split("#")

print(x)

Try it Yourself »

Example

Split the string into a list with max 2 items:

txt = "apple#banana#cherry#orange"

# setting the maxsplit parameter to 1, will return a list with 2 elements!
x = txt.split("#", 1)

print(x)

Try it Yourself »


❮ String Methods


Given a string, write a Python program to split the characters of the given string into a list using Python.

Examples:  

Input : geeks
Output : ['g', 'e', 'e', 'k', 's']

Input : Word
Output : ['W', 'o', 'r', 'd']

Method 1: Split a string into a Python list using unpack(*) method

The act of unpacking involves taking things out, specifically iterables like dictionaries, lists, and tuples.

Python3

string = "geeks"

print([*string])

Output: 

['g', 'e', 'e', 'k', 's']

Method 2: Split a string into a Python list using a loop

Here, we are splitting the letters using the native way using the loop and then we are appending it to a new list.

Python3

string = 'geeksforgeeks'

lst = []

for letter in string:

    lst.append(letter)

print(lst)

Output:

['g', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'g', 'e', 'e', 'k', 's']

Method 3: Split a string into a Python list using List Comprehension

This approach uses list comprehension to convert each character into a list. Using the following syntax you can split the characters of a string into a list.

Python3

string = "Geeksforgeeks"

letter = [x for x in string]

print(letter)

Output: 

['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'g', 'e', 'e', 'k', 's']

Method 4: Split a string into a Python list using a list() typecasting

Python provides direct typecasting of strings into a list using Python list().

Python3

def split(word):

    return list(word)

word = 'geeks'

print(split(word))

Output: 

['g', 'e', 'e', 'k', 's']

Method 5: Split a string into a Python list using extend()

Extend iterates over its input, expanding the list, and adding each member.

Python3

string = 'Geeks@for'

lst = []

lst.extend(string)

print(lst)

Output: 

['G', 'e', 'e', 'k', 's', '@', 'f', 'o', 'r']

Sometimes we need to work with just the lists and hence strings might need to be converted into lists. It has to be converted in list of characters for certain tasks to be performed. This is generally required in Machine Learning to preprocess data and text classifications. Let’s discuss certain ways in which this task is performed.

Method #1: Using list slicing

List slicing can be used for this particular purpose, in which we assign to each index element of list the next occurring character of string using the slice operation. 

Python3

test_string = "GeeksforGeeks"

print("The original string is : " + str(test_string))

res = []

res[:] = test_string

print("The resultant list of characters : " + str(res))

Output

The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']

Method #2: Using list() 

The most concise and readable way to perform splitting is to type case string into list and the splitting of list is automatically handled internally. This is recommended method to perform this particular task. 

Python3

test_string = "GeeksforGeeks"

print("The original string is : " + str(test_string))

res = list(test_string)

print("The resultant list of characters : " + str(res))

Output

The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']

Method #3: Using map() + lambda

This is yet another way to perform this particular task. Though not recommended but can be used in certain situations. But drawback is readability of code gets sacrificed.  

Python3

test_string = "GeeksforGeeks"

print ("The original string is : " + str(test_string))

res = list(map(lambda i:i, test_string))

print ("The resultant list of characters : " + str(res))

Output

The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']

Method#4: Using join() + split() method

We can use above mention method to split the string in list of character. Join is used to add the space between the character of string and split is used to split the character between space in string. 

Python3

test_string = "GeeksforGeeks"

print("The original string is : " + str(test_string))

test_string = " ".join(test_string)

res = test_string.split(" ")

print("The resultant list of characters : " + str(res))

Output

The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']

Method#5: Using re.findall() method: We can use above mention method to find all character in the string and form a list of characters.

Python

import re

test_string = "GeeksforGeeks"

print("The original string is : " + str(test_string))

res = re.findall(r'[a-zA-z]', test_string)

print("The resultant list of characters : " + str(res))

Output

The original string is : GeeksforGeeks
The resultant list of characters : ['G', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'G', 'e', 'e', 'k', 's']


How do you split a string into a list of characters in Python?

Use the list() class to split a string into a list of characters, e.g. my_list = list(my_str) . The list() class will convert the string into a list of characters.

How do you split a list by delimiter in Python?

The split() method of the string class is fairly straightforward. It splits the string, given a delimiter, and returns a list consisting of the elements split out from the string. By default, the delimiter is set to a whitespace - so if you omit the delimiter argument, your string will be split on each whitespace.

How do you split items in a list in Python?

To split the elements of a list in Python: Use a list comprehension to iterate over the list. On each iteration, call the split() method to split each string. Return the part of each string you want to keep.

How do you split a string at a specific position in Python?

Splitting Strings with the split() Function We can specify the character to split a string by using the separator in the split() function. By default, split() will use whitespace as the separator, but we are free to provide other characters if we want.