Python - Group Sublists by another List Last Updated : 20 Feb, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report Sometimes, while working with lists, we can have a problem in which we need to group all the sublists, separated by elements present in different list. This type of custom grouping is uncommon utility but having solution to these can always be handy. Lets discuss certain way in which this task can be performed. Method #1 : Using loop + generator(yield) This is brute force way in which this task can be performed. In this, we iterate the list and make groups dynamically using yield. We keep track of elements occurred and restart list when we find element in second list. Python3 # Python3 code to demonstrate # Group Sublists by another List # using loop + generator(yield) # helper function def grp_ele(test_list1, test_list2): temp = [] for i in test_list1: if i in test_list2: if temp: yield temp temp = [] yield i else: temp.append(i) if temp: yield temp # Initializing lists test_list1 = [8, 5, 9, 11, 3, 7] test_list2 = [9, 11] # printing original lists print("The original list 1 is : " + str(test_list1)) print("The original list 2 is : " + str(test_list2)) # Group Sublists by another List # using loop + generator(yield) res = list(grp_ele(test_list1, test_list2)) # printing result print ("The Grouped list is : " + str(res)) Output : The original list 1 is : [8, 5, 9, 11, 3, 7] The original list 2 is : [9, 11] The Grouped list is : [[8, 5], 9, 11, [3, 7]] Time Complexity: O(n*n)Auxiliary Space: O(n) Comment More infoAdvertise with us Next Article Python - Merge elements of sublists M manjeet_04 Follow Improve Article Tags : Python Python Programs Python list-programs Python-list-of-lists Practice Tags : python Similar Reads Check for Sublist in List-Python The task of checking for a sublist in a list in Python involves determining whether a given sequence of elements (sublist) appears consecutively within a larger list. This is a common problem in programming, particularly in data processing and pattern matching. For example, if we have a list [5, 6, 4 min read Python | Group List on K character 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 3 min read Python - Group list of tuples to dictionary The task is to convert a list of tuples into a dictionary, where each tuple consists of two elements. The first element of each tuple becomes the key and the second element becomes the value. If a key appears multiple times, its corresponding values should be grouped together, typically in a list.Fo 3 min read Python - Merge elements of sublists In Python, we often need to combine multiple sublists into a single list. This can be useful when dealing with data that is organized in smaller chunks, like rows in a table or pages in a document. Using itertools.chain()itertools.chain() function is one of the most efficient ways to merge sublists. 4 min read Python - Merge elements of sublists In Python, we often need to combine multiple sublists into a single list. This can be useful when dealing with data that is organized in smaller chunks, like rows in a table or pages in a document. Using itertools.chain()itertools.chain() function is one of the most efficient ways to merge sublists. 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 Like