Python - compare nested lists and append matches to new list?
Posted
by Seafoid
on Stack Overflow
See other posts from Stack Overflow
or by Seafoid
Published on 2010-03-29T14:36:17Z
Indexed on
2010/03/29
14:43 UTC
Read the original article
Hit count: 377
Hi,
I wish to compare to nested lists of unequal length. I am interested only in a match between the first element of each sub list. Should a match exist, I wish to add the match to another list for subsequent transformation into a tab delimited file. Here is an example of what I am working with:
x = [['1', 'a', 'b'], ['2', 'c', 'd']]
y = [['1', 'z', 'x'], ['4', 'z', 'x']]
match = []
def find_match():
for i in x:
for j in y:
if i[1] == j[1]:
match.append(j)
return match
This results in a series of empty lists.
Is it better to use tuples and/or tuples of tuples for the purposes of comparison?
Any help is greatly appreciated.
Regards, Seafoid.
© Stack Overflow or respective owner