Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python | Find common elements in list of lists
Next article icon

Uncommon Elements in Lists of List – Python

Last Updated : 10 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

We are given two lists of lists and our task is to find the sublists that are uncommon between them. (a sublist is considered uncommon if it appears in only one of the lists and not in both.) For example: a = [[1, 2], [3, 4], [5, 6]] and b = [[3, 4], [5, 7], [1, 2]] then the output will be [[5, 6], [5, 7]]

Using Set Operations with Tuple Conversion

In this method we convert each sublist in “a” and “b” to a tuple so that they become hashable and can be added to sets and then compute the symmetric difference between these sets to find the sublists that appear only in one of them. Finally, we convert the resulting tuples back to lists. (Note: this method does not preserve the original order.)

Python
a = [[1, 2], [3, 4], [5, 6]] b = [[3, 4], [5, 7], [1, 2]]  sa = set(tuple(x) for x in a)         # Convert each sublist in 'a' to a tuple and create a set sb = set(tuple(x) for x in b)         # Convert each sublist in 'b' to a tuple and create a set ut = sa.symmetric_difference(sb)      # Get the symmetric difference between the two sets res = [list(x) for x in ut]           # Convert the tuples back to lists  print(res) 

Output
[[5, 6], [5, 7]] 

Explanation:

  • Each sublist in a and b is converted into a tuple, making them hashable and allowing us to form sets (sa and sb) and then we compute the symmetric difference of sa and sb to find the tuples that are unique to either set.
  • Resulting tuples are converted back to lists to obtain the final result res containing the uncommon sublists.

Using collections.Counter

In this method we are using collections.Counter to count the occurrences of each sublist from the combined lists. Since lists are unhashable hence we first convert each sublist to a tuple and then we extract only those tuples that appear exactly once (i.e. are uncommon) and convert them back to lists.

Python
from collections import Counter  a = [[1, 2], [3, 4], [5, 6]] b = [[3, 4], [5, 7], [1, 2]]  c = Counter(tuple(x) for x in a + b)  # Count each sublist (converted to tuple) from both lists res = [list(x) for x in c if c[x] == 1]  # Convert tuples back to lists if they appear only once  print(res) 

Output
[[5, 6], [5, 7]] 

Explanation: We iterate over the counter and select only those sublists (converted back to lists) with a count of 1, which represent the uncommon sublists.

Using List Comprehension

In this method we iterate over each list using list comprehension and pick the sublists that are not found in the other list, this approach preserves the order of the original lists.

Python
a = [[1, 2], [3, 4], [5, 6]] b = [[3, 4], [5, 7], [1, 2]]  ua = [x for x in a if x not in b] # Get sublists from 'a' that are not in 'b' ub = [x for x in b if x not in a] # Get sublists from 'b' that are not in 'a' res = ua + ub # Combine the results  print(res)  

Output
[[5, 6], [5, 7]] 

Explanation:

  • First list comprehension iterates over each sublist in a and includes it in “ua” only if it does not appear in b.
  • similarly the second list comprehension gathers sublists from b that aren’t in a and finally we concatenate the two lists to get the final result.

Using filter and lambda

In this method we use the filter function combined with a lambda function to extract the sublists that do not appear in the other list.

Python
a = [[1, 2], [3, 4], [5, 6]] b = [[3, 4], [5, 7], [1, 2]]  ua = list(filter(lambda x: x not in b, a))  # Filter sublists from 'a' that are not in 'b' ub = list(filter(lambda x: x not in a, b))  # Filter sublists from 'b' that are not in 'a' res = ua + ub  # Combine the filtered sublists  print(res) 

Output
[[5, 6], [5, 7]] 

Explanation:

  • lambda function checks for each sublist in a whether it is not in b (and vice versa for b).
  • filter function applies this condition to generate filtered lists ua and ub.
  • At last we combine ua and ub to obtain the final result containing the uncommon sublists.


Next Article
Python | Find common elements in list of lists
author
manjeet_04
Improve
Article Tags :
  • Python
  • Python Programs
  • Python list-programs
Practice Tags :
  • python

Similar Reads

  • Python | Remove given element from list of lists
    The deletion of elementary elements from list has been dealt with many times, but sometimes rather than having just a one list, we have list of list where we need to perform this particular task. Having shorthands to perform this particular task can help. Let's discuss certain ways to perform this p
    6 min read
  • Python | Find common elements in list of lists
    The problem of finding the common elements in list of 2 lists is quite a common problem and can be dealt with ease and also has been discussed before many times. But sometimes, we require to find the elements that are in common from N lists. Let's discuss certain ways in which this operation can be
    6 min read
  • Flatten a List of Lists in Python
    Flattening a list of lists means turning a nested list structure into a single flat list. This can be useful when we need to process or analyze the data in a simpler format. In this article, we will explore various approaches to Flatten a list of Lists in Python. Using itertools.chain itertools modu
    3 min read
  • Python | Check if element exists in list of lists
    Given a list of lists, the task is to determine whether the given element exists in any sublist or not. Given below are a few methods to solve the given task. Method #1: Using any() any() method return true whenever a particular element is present in a given iterator. C/C++ Code # Python code to dem
    5 min read
  • Python | Sorting list of lists with similar list elements
    Sorting has always been a key operation that is performed for many applications and also as a subproblem to many problems. Many variations and techniques have been discussed and their knowledge can be useful to have while programming. This article discusses the sorting of lists containing a list. Le
    5 min read
  • Python - Elements Lengths in List
    Sometimes, while working with Python lists, can have problem in which we need to count the sizes of elements that are part of lists. This is because list can allow different element types to be its members. This kind of problem can have application in many domains such has day-day programming and we
    6 min read
  • Python | Convert list of tuples into list
    In Python we often need to convert a list of tuples into a flat list, especially when we work with datasets or nested structures. In this article, we will explore various methods to Convert a list of tuples into a list. Using itertools.chain() itertools.chain() is the most efficient way to flatten a
    3 min read
  • Python - Convert a list into tuple of lists
    When working with data structures in Python, there are times when we need to convert a list into a tuple of smaller lists. For example, given a list [1, 2, 3, 4, 5, 6], we may want to split it into a tuple of two lists like ([1, 2, 3], [4, 5, 6]). We will explore different methods to achieve this co
    3 min read
  • Remove Multiple Elements from List in Python
    In this article, we will explore various methods to remove multiple elements from a list in Python. The simplest way to do this is by using a loop. A simple for loop can also be used to remove multiple elements from a list. [GFGTABS] Python a = [10, 20, 30, 40, 50, 60, 70] # Elements to remove remov
    3 min read
  • Python | Convert column to separate elements in list of lists
    There are instances in which we might require to extract a particular column of a Matrix and assign its each value as separate entity in list and this generally has a utility in Machine Learning domain. Let's discuss certain ways in which this action can be performed.Method #1 : Using list slicing a
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences