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.
- 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.
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  to choose the first element myList = [i.split("\n") for i in lstMsg] print("After splits of the List") print(myList) # If you want to get the element after \n, you can use  myList1 = [i.split("\n") for i in lstMsg] print(myList1)
After splits of the list ['Welcome', 'To', 'Learn', 'Share', 'IT'] ['20981.ab', 's20021.o', '0139847', '109221a', '2209883a']
We can make a list become a new list like [‘Welcome’, ‘ To’, ‘Learn’, ‘ Share’, ‘IT’] by for loop.
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)
['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.
- 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.
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)
After split element in list ['Welcome', ' To', 'Learn', ' Share', 'IT']
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:
- Convert a Map object to a List in Python
- Create a list with same value repeated N times in Python
- Join a list of lists in Python
Name of the university: HUSC