Open In App

Python – Group Sublists by another List

Last Updated : 20 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
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
        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)


Similar Reads

Python | Remove sublists that are present in another sublist
Given a list of lists, write a Python program to remove sublists from the given list of lists that are present in another sublist. Examples: Input : [['a', 'b', 'c'], ['a', 'c'], ['a', 'b', 'c'], ['d']] Output : [['a', 'b', 'c'], ['d']] Input : [[1], [1, 2], [1, 2, 3], [0], [0, 1]] Output : [[1, 2, 3], [0, 1]] Approach #1: Using Python Set (If orde
4 min read
Print all sublists of a list in Python
Given a list, print all the sublists of a list. Python provides efficient and elegant ways to generate and manipulate sublists. In this article, we will explore different methods how to print all sublists of a given list. Examples: Input: list = [1, 2, 3] Output: [[], [1], [1, 2], [1, 2, 3], [2], [2, 3], [3]]Input: [1, 2, 3, 4] Output: [[], [1], [1
3 min read
Python | Sort all sublists in given list of strings
Given a list of lists, the task is to sort each sublist in the given list of strings. Example: Input: lst = [['Machine', 'London', 'Canada', 'France'], ['Spain', 'Munich'], ['Australia', 'Mandi']] Output: flist = [['Canada', 'France', 'London', 'Machine'], ['Munich', 'Spain'], ['Australia', 'Mandi']] There are multiple ways to sort each list in alp
3 min read
Python | Sort list of lists by the size of sublists
Given a list of lists, the task is to sort a list on the basis of size of sublists. Let's discuss a few methods to do the same. Method #1: Using sort C/C++ Code # Python code to demonstrate # sort list of list # on the basis of size of sublist ini_list = [[1, 2, 3], [1, 2], [1, 2, 3, 4], [1, 2, 3, 4, 5], [2, 4, 6]] # printing initial ini_list print
5 min read
Python | Count the sublists containing given element in a list
Given a list of lists, write a Python program to count the number of sublists containing the given element x. Examples: Input : lst = [1, 3, 5], [1, 3, 5, 7], [1, 3, 5, 7, 9]] x = 1 Output : 3 Input : lst = (['a'], ['a', 'c', 'b'], ['d']) x = 'a' Output : 2 Approach #1 : Naive Approach Count the number of lists containing x. Initialize count to 0,
4 min read
Python | Count unique sublists within list
Given a list of lists, the task is to find the count of unique sublists within list. Examples: Input: [['Geek', 'for', 'geeks'], ['geeks', 'for'], ['for', 'Geeks', 'geek'], ['Geek', 'for', 'geeks']] Output: {('geeks', 'for'): 1, ('for', 'Geeks', 'geek'): 1, ('Geek', 'for', 'geeks'): 2} Below are some ways to achieve the task. Method #1: Using Itera
2 min read
Python | Split a list into sublists of given lengths
The problem of splitting a list into sublists is quite generic but to split in sublist of given length is not so common. Given a list of lists and list of length, the task is to split the list into sublists of given length. Example: Input : Input = [1, 2, 3, 4, 5, 6, 7] length_to_split = [2, 1, 3, 1] Output: [[1, 2], [3], [4, 5, 6], [7]] Method #1:
2 min read
Python | Split list of strings into sublists based on length
Given a list of strings, write a Python program to split the list into sublists based on string length. Examples: Input : ['The', 'art', 'of', 'programming'] Output : [['of'], ['The', 'art'], ['programming']] Input : ['Welcome', 'to', 'geeksforgeeks'] Output : [['to'], ['Welcome'], ['geeksforgeeks']] Approach #1 : Naive A naive approach for the abo
3 min read
Python | Remove repeated sublists from given list
Given a list of lists, write a Python program to remove all the repeated sublists (also with different order) from given list. Examples: Input : [[1], [1, 2], [3, 4, 5], [2, 1]] Output : [[1], [1, 2], [3, 4, 5]] Input : [['a'], ['x', 'y', 'z'], ['m', 'n'], ['a'], ['m', 'n']] Output : [['a'], ['x', 'y', 'z'], ['m', 'n']] Approach #1 : Set comprehens
2 min read
Python | Merge elements of sublists
Given two lists containing sublists, the task is to merge elements of sublist of two lists in a single list. Examples: Input: list1 = [[1, 20, 30], [40, 29, 72], [119, 123, 115]] list2 = [[29, 57, 64, 22], [33, 66, 88, 15], [121, 100, 15, 117]] Output: [[1, 20, 30, 29, 57, 64, 22], [40, 29, 72, 33, 66, 88, 15], [119, 123, 115, 121, 100, 15, 117]] M
3 min read