
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Print Common Elements in All Sublists in Python
The Python language is primarily composed of different data structures and from it the list data structure is the most used one. The lists can hold elements of different data types and when they are assigned with some values they cannot be changed. The lists can even hold other lists within the square brackets "[]". When two lists are defined to work on some platform, there may be some relations between them, and using this code the Python program makes it possible.
Printing common elements in the sublist
Let's assume a list containing 10 elements and it can also be represented in the form of sublists like 3 elements in one sublist and the remaining seven elements in another sublist.
Syntax
list1 = [[1, 2, 3], [4, 5, 6, 7, 8, 9, 10]]
The basic syntax contains the list data structure along with the sublists.
Approach
Approach 1 ? Using the recursive function
Approach 2 ? Using the reduce function
Approach 3 ? Using the iteration method
Approach 1: Python Program to print common elements in all sublists using recursive function
The common elements from the sublists of the list are printed with the help of a recursive function, which the function calls itself recursively.
Algorithm
Step 1 ? Create the function common_elements().
Step 2 ? When the list's length is equal to 1, it sets the list's first element.
Step 3 ? Otherwise, it sets the intersection function to match the sublists for finding the common element using the slicing method.
Step 3 ? It returns the statement with common elements.
Example
#function is defined with a list argument def common_elements(list1): if len(list1) == 1: return set(list1[0]) else: return set(list1[0]).intersection(common_elements(list1[1:])) #initializing the sublist inside the list data structure list1 = [[2,3,4,5], [2,5], [2,10,5], [2,8,5,6], [2,10,9,5]] common_elements = common_elements(list1) #the length of the list should be greater than 0 if len(common_elements)>0: print("The common elements in the given sublists is:",common_elements) else: print("The given list does not contain common elements! ")
Output
The common elements in the given sublist is: {2, 5}
Approach 2: Python Program to print common elements in all sublists using reduce function
In the method to print the common elements without any complex steps, the functools module is imported to use the reduce function.
Algorithm
Step 1 ? Import the required module.
Step 2 ? The list data structure is initialized with the sublists to find the common elements among them.
Step 3 ? The primary function behind this problem is achieved by the reduce and intersection functions.
Step 4 ? The map function simply maps the elements and converts each sublist into a set.
Step 5 ? Depending on the input defined, the print statement will return the common elements among the sublists.
Example
#importing the module from functools import reduce #initializing the sublist inside the list data structure list1 = [[2,3,4,5], [2,5], [2,10,5], [2,8,5,6], [2,10,9,5]] #with the help of the intersection function to match the elements among sublists common_elements = reduce(set.intersection,map(set,list1)) #the length of the list should be greater than 0 if len(common_elements)>0: print("The common elements in the given sublists is:",common_elements) else: print("The given list does not contain common elements! ")
Output
The common elements in the given sublist is: {2, 5}
Approach 3: Python Program to print common elements using for loop iteration method
The iteration through the lists makes the output print all possibilities of the common elements between the sublists.
Algorithm
Step 1 ? Creation of function with one list argument.
Step 2 ? Then create an empty set and take the first element of the array as list1[0].
Step 3 ? Use for loop for iteration.
Step 4 ? Verify the condition whether the length is greater than zero or not.
Step 5 ? Invoke the function common_element, and the output statement is printed.
Example
#defining the function with one list argument def common_element(list1): #creating an empty set and taking the first element of the array reference_set=set(list1[0]) #for is used to iterate through the given list for num in range(1,len(list1)): #Adding the common element from the list to the created set fresh_set=set(tuple(list1[num])) reference_set&=fresh_set #To check whether the length of the list greater than 0 if len(reference_set) > 0: print("The common elements in the given sublists is:",reference_set) else: print("The given list does not contain common elements! ") #initializing the list of sublists list1 = [[11,3,12,5], [12,11], [12,11,5], [12,11,5,6], [12,11,9,5]] #calling the function to print the common elements common_element(list1)
Output
The common elements in the given sublist is: {11, 12}
Conclusion
To find the common elements in a sublist, one should know how the sublist is assigned and its basic structure. The lists contain elements that are separated by common within the square brackets. The approaches are explained to print the common elements in the sublists of the list using the modules, recursive function, and the simple iteration method.