Python program to convert a list to a set based on a common element
Last Updated :
21 Apr, 2023
Given a list of lists, the task is to write a python program that can convert each sublist to a set and combine to sublists into one set if they have a common element. The output printed will be a list of sets.
Input : test_list = [[15, 14, 12, 18], [9, 5, 2, 1], [4, 3, 2, 1], [19, 11, 13, 12]]
Output : [{11, 12, 13, 14, 15, 18, 19}, {1, 2, 3, 4, 5, 9}]
Explanation : List 1 and list 4 had 12 in common, hence all got merged to one.
Input : test_list = [[15, 14, 12, 18], [9, 5, 2, 1], [4, 3, 2, 1], [19, 11, 13, 22]]
Output : [{18, 12, 14, 15}, {1, 2, 3, 4, 5, 9}, {11, 19, 13, 22}]
Explanation : List 2 and list 3 had 1, 2 in common, hence all got merged to one.
Method : Using recursion and union()
In this, we perform the task of getting all containers having like elements using union(), depending upon conditions. Recursion is used to perform similar task to most lists required.
Example:
Python3
# utility function
def common_set(test_set):
for idx, val in enumerate(test_set):
for j, k in enumerate(test_set[idx + 1:], idx + 1):
# getting union by conditions
if val & k:
test_set[idx] = val.union(test_set.pop(j))
return common_set(test_set)
return test_set
# utility function
# initializing lists
test_list = [[15, 14, 12, 18], [9, 5, 2, 1], [4, 3, 2, 1], [19, 11, 13, 12]]
# printing original list
print("The original list is : " + str(test_list))
test_set = list(map(set, test_list))
# calling recursive function
res = common_set(test_set)
# printing result
print("Common element groups : " + str(res))
Output:
The original list is : [[15, 14, 12, 18], [9, 5, 2, 1], [4, 3, 2, 1], [19, 11, 13, 12]]
Common element groups : [{11, 12, 13, 14, 15, 18, 19}, {1, 2, 3, 4, 5, 9}]
Time Complexity: O(n*n)
Auxiliary Space: O(n)
Approach#2: Using set intersection
The approach used in this code is to iterate over each list in the input list, convert it to a set, and then find all other lists that have common elements with it. Then, it merges all the common lists into one set and removes them from the input list. This process is repeated until there are no more lists left in the input list.
- Create an empty list result to store the final sets.
- While the input list test_list is not empty:
- Remove the first list from test_list and convert it to a set last.
- Find all other lists in test_list that have common elements with last.
- Merge all common lists into one set and remove them from test_list.
- Add the merged set to the result.
- Return result.
Python3
# Python program for the above approach
# Function to reverse the list into set
def convert_list_to_set(test_list):
# Create an empty list to store the result
result = []
# Iterate over each list in the input list
while test_list:
# Remove the first list from the input
# list and convert it to a set
lst = set(test_list.pop(0))
# Find all other lists that have
# common elements with the first list
common_lists = [l for l in test_list if lst.intersection(set(l))]
# Merge all common lists into one set
# and remove them from the input list
for common_lst in common_lists:
lst |= set(common_lst)
test_list.remove(common_lst)
# Add the merged set to the result
result.append(lst)
return result
# Driver Code
test_list = [[15, 14, 12, 18], [9, 5, 2, 1], [4, 3, 2, 1], [19, 11, 13, 12]]
print(convert_list_to_set(test_list))
Output[{11, 12, 13, 14, 15, 18, 19}, {1, 2, 3, 4, 5, 9}]
Time complexity: O(n^2), where n is the length of the input list test_list. This is because we are iterating over each list in test_list and then iterating over the remaining lists to find the common elements.
Space complexity: O(n^2), where n is the length of the input list test_list. This is because we are creating sets for each list in test_list, and we are also creating a list of common lists for each lst in test_list.
Similar Reads
Python Program to Find Most common elements set Given a List of sets, the task is to write a Python program tocompare elements with argument set, and return one with maximum matching elements. Examples: Input : test_list = [{4, 3, 5, 2}, {8, 4, 7, 2}, {1, 2, 3, 4}, {9, 5, 3, 7}], arg_set = {9, 6, 5, 3}Output : {9, 3, 5, 7}Explanation : Resultant
5 min read
Python program to convert Set into Tuple and Tuple into Set Let's see how to convert the set into tuple and tuple into the set. For performing the task we are use some methods like tuple(), set(), type(). tuple(): tuple method is used to convert into a tuple. This method accepts other type values as an argument and returns a tuple type value.set(): set metho
7 min read
Python - Print all common elements of two lists Finding common elements between two lists is a common operation in Python, especially in data comparison tasks. Python provides multiple ways to achieve this, from basic loops to set operations. Let's see how we can print all the common elements of two listsUsing Set Intersection (Most Efficient)The
3 min read
Convert Set of Tuples to a List of Lists in Python Sets and lists are two basic data structures in programming that have distinct uses. It is sometimes necessary to transform a collection of tuples into a list of lists. Each tuple is converted into a list throughout this procedure, and these lists are subsequently compiled into a single, bigger list
3 min read
Time Complexity to Convert a Set to List in Python Concerting a set to a list involves iterating through the elements of the set and adding them to a list. The time complexity of this operation is linear and depends on the size of the set. Let's denote the size of the set as n. The time complexity to convert a set to a list generally takes O(n) time
3 min read
Python Convert Set To List Without Changing Order Sets in Python are inherently unordered collections. If the order of the elements is important, the input set must have been created in a specific order, such as by using an ordered iterable like a list. Let us explore different methods to convert a set to a list without changing its apparent order.
2 min read