How to Filter and De‑duplicate Python Lists Using itertools.chain and List Comprehensions
This article demonstrates how to filter and deduplicate nested Python lists using itertools.chain with break statements and an alternative list‑comprehension approach, providing clear code examples and explanations to help readers solve common list‑handling problems.
Hello, I'm an advanced Python user.
1. Introduction
Yesterday in the Python community "NicePlus" a member asked a basic question about processing a list of lists. The original data are:
l1 = [['a','b','c'], [1,2,3], ['c','c','c']]
l2 = [[1,1,1], ['a','a','a'], ['c',0,0,0]]
l3 = [['c','c','c'], [5,6,7], [7,3,4], [0,'a']]2. Implementation
The first solution uses itertools.chain to iterate over all sub‑lists and a break statement to stop after the first matching element, effectively removing duplicates.
from itertools import chain
l1 = [['a','b','c'], [1,2,3], ['c','c','c']]
l2 = [[1,1,1], ['a','a','a'], ['c',0,0,0]]
l3 = [['c','c','c'], [5,6,7], [7,3,4], [0,'a']]
l = []
for i in chain(l1, l2, l3):
for j in i:
if j in ('a','b','c'):
l.append(i)
break
print(l)The output shows the sub‑lists that contain any of the characters 'a', 'b', or 'c'.
An alternative, more compact solution uses a list comprehension:
l1 = [['a','b','c'], [1,2,3], ['c','c','c']]
l2 = [[1,1,1], ['a','a','a'], ['c',0,0,0]]
l3 = [['c','c','c'], [5,6,7], [7,3,4], [0,'a']]
all_list = l1 + l2 + l3
l = []
res = [l.append(i) for i in all_list for j in i if j in ('a','b','c')]
final_res = []
result = [final_res.append(item) for item in l if item not in final_res]
print(final_res)Although slightly redundant, this version achieves the same result.
3. Summary
The article walks through a common Python list‑handling problem, presents two concrete implementations, and demonstrates how to filter and deduplicate nested lists effectively.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
