How to Merge and Filter Python Lists Using itertools.chain and List Comprehensions
This article walks through a Python fan's list‑processing question, showing how to combine multiple nested lists with itertools.chain, filter elements, eliminate duplicates, and then achieve the same result more concisely using a list‑comprehension approach.
1. Introduction
In a Python discussion group a user asked how to process several nested lists to extract sub‑lists that contain any of the characters 'a', 'b', or 'c'. The article presents a step‑by‑step solution.
2. Original Data
The three source lists 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']]These lists are visualised in the original article (image omitted for brevity).
3. First Implementation
The solution uses itertools.chain to iterate over all sub‑lists and a break statement to stop after the first matching element, thereby avoiding duplicate entries:
from itertools import chain
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 at least one of the target characters.
4. List‑Comprehension Alternative
A more compact version combines the three lists, then uses a nested list comprehension to collect matching sub‑lists and a second pass to remove duplicates:
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 code produces the same correct result.
5. Conclusion
The article demonstrates two practical ways to merge and filter Python lists, helping the original asker solve the problem efficiently.
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.
