Friday, December 27, 2024
Google search engine
HomeLanguagesPython | Ways to Split the list by some value

Python | Ways to Split the list by some value

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




# Python code to split the list
# by some value into two lists.
 
# List initialisation
list = ['Geeks', 'forLazyroar', 'is a', 'portal', 'for Geeks']
 
# Splitting list into first half
first_list = list[:list.index('forLazyroar')]
 
# Splitting list into second half
second_list = list[list.index('forLazyroar')+1:]
 
# Printing first list
print(first_list)
 
# Printing second 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




# Python code to split the list
# by some value into two lists.
 
# Importing
from itertools import dropwhile
 
# List initialisation
lst = ['Geeks', 'forLazyroar', 'is a', 'portal', 'for Geeks']
 
# Using dropwhile to split into second list
second_list = list(dropwhile(lambda x: x != 'forLazyroar', lst))[1:]
 
# Using set to get difference between two lists
first_list = set(lst)-set(second_list)
 
# removing 'split' string
first_list.remove('forLazyroar')
 
# converting to list
first_list = list(first_list)
 
# Printing first list
print(first_list)
 
# Printing second 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
 
# List initialization
my_list = ['Geeks', 'forLazyroar', 'is a', 'portal', 'for Geeks']
 
# Splitting the list into two parts
condition = lambda x: x != 'forLazyroar'
first_list = list(itertools.takewhile(condition, my_list))
second_list = list(itertools.dropwhile(condition, my_list))[1:]
 
# Printing the two parts of the list
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.

RELATED ARTICLES

Most Popular

Recent Comments