# How To Split The Elements Of A List In Python

There are some ways to split the elements of a list in Python. We have effectively tried using some methods like split() and creating the function to split elements. Let’s learn about it with the explanation and examples below.

## Split the elements of a list in Python

### Method 1: Use the split() method

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

Syntax:

string.split(separator, maxsplit)

Parameters:

• separator: Default white space is a separator used to split the string.
• maxsplit: Optional, the number of splits you want. The value default of maxsplit is -1.

Return value: The return value is a list.

Example 1:

In this example, we will use each string in the list and then split the string, then we choosing an element before or after the split character.

lstMsg = [
"Welcome\n20981.ab",
"To\ns20021.o",
"Learn\n0139847",
"Share\n109221a",
"IT\n2209883a",
]

# Splitting the string a "\n" and use [0] to choose the first element
myList = [i.split("\n")[0] for i in lstMsg]
print("After splits of the List")
print(myList)

# If you want to get the element after \n, you can use [1]
myList1 = [i.split("\n")[1] for i in lstMsg]
print(myList1)

Output:

After splits of the list
['Welcome', 'To', 'Learn', 'Share', 'IT']
['20981.ab', 's20021.o', '0139847', '109221a', '2209883a']

Example 2:

We can make a list become a new list like [‘Welcome’, ‘ To’, ‘Learn’, ‘ Share’, ‘IT’] by for loop.

Code:

lstMsg = ["Welcome. To", "Learn. Share", "IT"]
newList = []

# Loop to all elements in the list, split, and add the element to the newList
for i in lstMsg:
newList += i.split(".")

print("After split . in lstMsg")
print(newList)

Output:

['Welcome', ' To', 'Learn', ' Share', 'IT']

### Method 2: Create the function to splits the elements in the list

We use enumerate() method to generate an enumerated object, and then we use the index() method to get the first position of the object.

Syntax:

enumerate(iterable, start=0)

Parameters:

• Iterable: object supports iteration
• Start: the index value from which the counter is to be started. The default value is 0.

Return value: The enumerate() method will return an object.

Example:

In this example, we will enumerate the list to x. Then loop to all elements in x and split “\n”. After that, we use the index() method to return the position at the first occurrence of the specified value.

lstMsg = [
"Welcome\n20981.ab",
"To\ns20021.o",
"Learn\n0139847",
"Share\n109221a",
"IT\n2209883a",
]

# Enumerate list to x and loop to all element in x
for i, x in enumerate(lstMsg):
if "\n" in x:
lstMsg[i] = x[: x.index("\n")]

print("After split element in lstMsg")
print(lstMsg)

Output:

After split element in list
['Welcome', ' To', 'Learn', ' Share', 'IT']

## Summary

Our tutorial has guided you through different ways to split the elements of a list in Python by using the split() method and creating the function to split elements. We always hope this tutorial is helpful for you. If you have any questions, please let us know by leaving your comment in this tutorial. Thank you.

Maybe you are interested: