Comparing nested list Python -
i guess code explain i'm going for...
list1 = [("1", "item 1"), ("2", "item 2"), ("3", "item 3"), ("4", "item 4")] list2 = [("1", "item 1"), ("2", "item 2"), ("4", "item 4")] newlist = [] i,j in list1: if not in list2[0]: entry = (i,j) newlist.append(entry) print(newlist)
if call nested tuples [i][j]
i want compare [i] once has been done want keep corresponding [j] value.
i have found lots of information regarding nested tuples on internet refer finding specific item.
i did use expression below, worked perfectly, seems similar, won't play ball.
for i,j in highscores: print("\tplayer:\t", j, "\tscore: ", i)
any apppreciated.
if understand correctly comment take newlist:
newlist = [("3", "item 3")]
you can using:
1) list comprehension:
newlist = [item item in list1 if item not in list2] print newlist
this give result:
[('3', 'item 3')]
2) use symmetric difference like:
l = set(list1).symmetric_difference(list2) newlist = list(l) print newlist
this give same result!
3) can use lambda function like:
unique = lambda l1, l2: set(l1).difference(l2) x = unique(list1, list2) newlist = list(x)
this produce same result!
4) oh, , last not least, using simple set properties:
newlist = list((set(list1)-set(list2)))
Comments
Post a Comment