Python | Group List on K character
Last Updated :
09 Apr, 2023
Sometimes, we may face an issue in which we require to split a list to list of list on the K character sent as deliminator. This kind of problem can be used to send messages or can be used in cases where it is desired to have list of list of native list. Let’s discuss certain ways in which this can be done.
Method #1: Using index() and list slicing The list slicing can be used to get the sublists from the native list and index function can be used to check for the K character which can potentially act as a separator. The drawback of this is that it only works for a single split i.e can only divide a list to 2 sublist.
Python3
# Python3 code to demonstrate
# Group List on K character
# using index() + list slicing
# initializing list
test_list = ['Geeks', 'for', 'M', 'Geeks', 1, 2]
# printing original list
print("The original list : " + str(test_list))
# initializing K
K = 'M'
# using index() + list slicing
# Group List on K character
temp_idx = test_list.index(K)
res = [test_list[: temp_idx], test_list[temp_idx + 1: ]]
# print result
print("The list of sublist after separation : " + str(res))
Output : The original list : ['Geeks', 'for', 'M', 'Geeks', 1, 2]
The list of sublist after separation : [['Geeks', 'for'], ['Geeks', 1, 2]]
Time Complexity: O(n) where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n) where n is the number of elements in the list “test_list”.
Method #2: Using loops The problem of the above proposed method can be solved using general loops and using brute force, in this we just look for K character and make a new list after that.
Python3
# Python3 code to demonstrate
# Group List on K character
# using loop
# initializing list
test_list = ['Geeks', 'M', 'for', 'M', 4, 5, 'M', 'Geeks', 'CS', 'M', 'Portal']
# printing original list
print("The original list : " + str(test_list))
# initializing K
K = 'M'
# using loop
# Group List on K character
temp = []
res = []
for ele in test_list:
if ele == K:
res.append(temp)
temp = []
else:
temp.append(ele)
res.append(temp)
# print result
print("The list of sublist after separation : " + str(res))
Output : The original list : ['Geeks', 'for', 'M', 'Geeks', 1, 2]
The list of sublist after separation : [['Geeks', 'for'], ['Geeks', 1, 2]]
Time Complexity: O(n*n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n), where n is the number of elements in the list “test_list”.
Method #3: Using itertools.groupby()
Python3
import itertools
#initializing list
test_list = ['Geeks', 'for', 'M', 'Geeks', 1, 2]
#printing original list
print("The original list : " + str(test_list))
#initializing K
K = 'M'
#using itertools.groupby()
#Group List on K character
res = [list(g) for k, g in itertools.groupby(test_list, lambda x: x == K) if not k]
#print result
print("The list of sublist after separation : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy
OutputThe original list : ['Geeks', 'for', 'M', 'Geeks', 1, 2]
The list of sublist after separation : [['Geeks', 'for'], ['Geeks', 1, 2]]
Time Complexity: O(n)
Auxiliary Space: O(n)
Similar Reads
Python - Groups Strings on Kth character Sometimes, while working with Python Strings, we can have a problem in which we need to perform Grouping of Python Strings on the basis of its Kth character. This kind of problem can come in day-day programming. Let's discuss certain ways in which this task can be performed. Method #1: Using loop Th
4 min read
Python - Group list by first character of string Sometimes, we have a use case in which we need to perform the grouping of strings by various factors, like first letter or any other factor. These types of problems are typical to database queries and hence can occur in web development while programming. This article focuses on one such grouping by
7 min read
Split String of list on K character in Python In this article, we will explore various methods to split string of list on K character in Python. The simplest way to do is by using a loop and split().Using Loop and split()In this method, we'll iterate through each word in the list using for loop and split it based on given K character using spli
2 min read
Python | Remove Kth character from strings list Sometimes, while working with data, we can have a problem in which we need to remove a particular column, i.e the Kth character from string list. String are immutable, hence removal just means re creating a string without the Kth character. Let's discuss certain ways in which this task can be perfor
7 min read
Python | Merge Range Characters in List Sometimes, we require to merge some of the elements as single element in the list. This is usually with the cases with character to string conversion. This type of task is usually required in development domain to merge the names into one element. Letâs discuss certain ways in which this can be perf
6 min read