In Python, lists are used to store multiple items/elements of any data type in a single variable. To split the list’s elements/items, various inbuilt functions, including some standard modules, are used in Python. For instance, the split() method, list comprehension, partition() function, etc., are used to split the list elements in Python.
This write-up will provide a detailed guide on splitting the elements of a list in Python using numerous examples. The following methods are discussed in this Python guide:
- Method 1: Using split() Method
- Method 2: Using List Comprehension
- Method 3: Using numpy.array_split() Function
- Method 4: Using partition() Method
So, let’s begin!
Method 1: Using split() Method
In the example given below, the “split()” method is used to split the elements of the list and retrieve the specific part of the list elements.
Code-1: (Split the list elements and return list of string)
list_value = ['1-Alex', '2-John', '3-David', '4-Lily']
output = [i.split('-')[1] for i in list_value]
print(output)
In the above code:
- The list contains combined numeric and character string values.
- The “split()” method is used along with list comprehension to split the numeric and string parts of the list elements and return the list of string values.
- From the above snippet, it can be seen that alphabetic strings are placed at index 1, and numeric strings are placed at index 0.
- Since we want to get only alphabetic strings from the given list. Therefore, we utilized the index “[1]” with the split() function.
Output:
The list of strings is created successfully in the above output.
In the code given below, the “split()” method is again used to split the list element and return the numeric strings instead of alphabetic strings.
Code-2: (Split the element of the list and return a list of numeric strings)
list_value = ['1-Alex', '2-John', '3-David', '4-Lily']
output = [i.split('-')[0] for i in list_value]
print(output)
In the above code:
- We used index position “0” instead of “1” with the split() method to get the list of numeric strings. The snippet below will clarify where we made changes in the code:
Output:
The above output shows the list of numeric strings.
In the code given below, the “split()” method is used to split the items of the list into nested lists. Skipping the index position from the split()function will return all the list elements as a nested list:
Code-3: (Split the element of the list and return nested list )
list_value = ['1-Alex', '2-John', '3-David', '4-Lily']
output = [i.split('-') for i in list_value]
print(output)
In the above code:
- The “split()” method is used along with list comprehension and for loop to split the elements of a list into the nested list. The “-” symbol is passed to the split() function as a separator.
Output:
The above output shows that the split() function splits the input list into nested lists.
Method 2: Using List Comprehension
In the example code given below, list comprehension is used along with string slicing to split the elements of the list according to the specified size.
Code:
list_value = ['John', 'Alex', 43, 90, 55, 34]
step_size = 3
# using list comprehension
new_list = [list_value[i:i + step_size] for i in range(0, len(list_value), step_size)]
print("\nGiven List: \n")
print(list_value)
print("\nAfter Splitting List: \n")
print(new_list)
In the above code:
- An alpha-numeric list is created in the program.
- The step size or each chunk size is stored in a variable named “step_size”.
- The “List Comprehension” is used along with the combination of the “for” loop to iterate over the list and split the elements of the list according to specified step size.
- The slicing of the list concept is also combined in the list comprehension to split the list element.
Output:
In the above output, the element of the list is split according to the specified step size.
Method 3: Using numpy.array_split() Function
In the example code given below, the “numpy.array_split()” function of the “numpy” package is used to split the given list into sublists based on the specified size.
Code:
import numpy
list_value = ['1','Alex', '2','John', '3','David', '4','Lily','5']
split_items = numpy.array_split(list_value, 3)
for array in split_items:
print(list(array))
In the above code:
- The “numpy” library is imported at the start to access the “numpy.array_split()” function.
- List values containing alpha-numeric strings are initialized and stored in a variable named “list_value”.
- The “numpy.array_split()” function takes the input list as the first argument and chunk size as the second argument.
- We passed “3” as a chunk size; therefore, the split() function splits the given list into three separate lists.
- We utilized the for loop to iterate over the obtained list. Within the “for” loop, we utilized the print() function to print the separated lists.
Output:
The above output shows that the array_split() function successfully splits the input list into different lists.
Method 4: Using partition() Method
In the example given below, the “partition()” method of the “toolz” module is used to split the elements of the list according to the specified size.
Code:
from toolz import partition
list_value = ['1','Alex', '2','John', '3','David', '4','Lily','5']
split_items = list(partition(3, list_value))
print(split_items)
In the above code:
- The “partition” method is imported from the “toolz” module.
- The “partition()” method, along with the “list()” function, takes the chunks size of the list and list variable as an argument and returns the list of elements according to the specified size.
Output:
The above output snippet shows that the list of separated elements was successfully created.
That’s it from this guide!
Conclusion
To split the elements of the list, the “split()” methods, “List Comprehension”, “numpy.array_split()”, and “partition()” methods are used in Python. The “split()” method splits the string list according to the specified separator and returns the list of strings. The list comprehension is also used along with the combination of string slicing to split the elements of the list. The “numpy.array_split()” and “partition()” methods are used to split the elements of the list easily and straightforwardly. This article provided a detailed guide on splitting the list’s elements in Python with multiple examples.