Given a list (may contain either strings or numbers), the task is to split the list by some value into two lists.
The approach is very simple. Split the first half of list by given value, and second half from the same value. There are multiple variations possible from this operation based on the requirement, like dropping the first/some element(s) in second half after the split value etc. Let’s see the different ways we can do this task.
Method #1: Using list index
Python3
list = [ 'Geeks' , 'forgeeks' , 'is a' , 'portal' , 'for Geeks' ]
first_list = list [: list .index( 'forgeeks' )]
second_list = list [ list .index( 'forgeeks' ) + 1 :]
print (first_list)
print (second_list)
|
Output:
['Geeks']
['is a', 'portal', 'for Geeks']
Time Complexity: O(n), where n is the length of the list test_list
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list
Method #2: Using dropwhile and set
Python3
from itertools import dropwhile
lst = [ 'Geeks' , 'forgeeks' , 'is a' , 'portal' , 'for Geeks' ]
second_list = list (dropwhile( lambda x: x ! = 'forgeeks' , lst))[ 1 :]
first_list = set (lst) - set (second_list)
first_list.remove( 'forgeeks' )
first_list = list (first_list)
print (first_list)
print (second_list)
|
Output:
['Geeks']
['is a', 'portal', 'for Geeks']
Method 3: Using the itertools.takewhile() and itertools.dropwhile() functions to split the list into two parts based on a condition.
Step-by-step approach:
- Import the itertools module.
- Define the original list to be split.
- Define a lambda function to be used as the condition for takewhile() and dropwhile().
- Use takewhile() to take elements from the list while the condition is true, and store them in a new list.
- Use dropwhile() to drop elements from the list while the condition is true, and store the remaining elements in a new list.
- Remove the first element of the second list, as it is the value that caused the splitting.
- Print the two parts of the original list.
Below is the implementation of the above approach:
Python
import itertools
my_list = [ 'Geeks' , 'forgeeks' , 'is a' , 'portal' , 'for Geeks' ]
condition = lambda x: x ! = 'forgeeks'
first_list = list (itertools.takewhile(condition, my_list))
second_list = list (itertools.dropwhile(condition, my_list))[ 1 :]
print (first_list)
print (second_list)
|
Output
['Geeks']
['is a', 'portal', 'for Geeks']
Time complexity: O(n), where n is the length of the list.
Auxiliary space: O(n), as we create two new lists to store the split parts of the original list.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
18 Apr, 2023
Like Article
Save Article