1

I guess the code will most easily explain what 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 = []

for i,j in list1:
    if i not in list2[0]:
        entry = (i,j)
        newlist.append(entry)

print(newlist)

if we call the nested tuples [i][j]

I want to compare the [i] but once this has been done I want to keep the corresponding [j] value.

I have found lots of information regarding nested tuples on the internet but most refer to finding a specific item.

I did recently use an expression below, which worked perfectly, this seems very similar, but it just won't play ball.

for i,j in highscores:
    print("\tPlayer:\t", j, "\tScore: ", i)

Any help would be much apppreciated.

4
  • 2
    Unfortunately, the code doesn't explain what you're going for :-). What do you want to get out of this? Commented Sep 12, 2016 at 22:06
  • This has been hard to describe .... I want to compare the first of the nested sequence. i.e. I want it to notice that in list1: "3" doesn't appear. But I want the item nested with it to also be retained. In short I'd like newlist to contain [("3", "Item 3")] Commented Sep 12, 2016 at 22:08
  • I don't doubt that. But if we can't tell what you're asking for, it's going to be hard to give you advice. Let's start off easy. Given list1 and list2 above, what would you like to be in newlist after everything is all finished? Commented Sep 12, 2016 at 22:10
  • sorry - i hit enter and didnt realise that would post not give a return Commented Sep 12, 2016 at 22:11

2 Answers 2

2

If I understand correctly from your comment you would like to take as newlist:

newlist = [("3", "Item 3")]

You can do this using:

1) list comprehension:

newlist = [item for item in list1 if item not in list2]
print newlist

This will give you as a result:

[('3', 'Item 3')]

2) You could also use symmetric difference like:

L = set(list1).symmetric_difference(list2)
newlist = list(L)
print newlist

This will also give you the same result!

3) Finally you can use a lambda function like:

unique = lambda l1, l2: set(l1).difference(l2)
x = unique(list1, list2)
newlist = list(x)

This will also produce the same result!

4) Oh, and last but not least, using simple set properties:

newlist = list((set(list1)-set(list2)))
Sign up to request clarification or add additional context in comments.

4 Comments

Wow! Yeah thats exactly right. That might take a while to get my head around. But works perfectly!
Can i ask why does 'item for item' get repeated at the beginning?
@Jup, Just updated the answer with a second way which may be a bit more easy ! Hope it will be of some help!
@Jup, The second item exists just to define that item refers to-is a member of list1 and It is just the way comprehension lists work (syntactically). There is a lot of staff in the internet about them and are a very interesting topic.
1

I think you just want to create a set of the first elements of list2, if you're only looking to compare the first element of the lists.

newlist = []
list2_keys = set(elem[0] for elem in list2)
for entry in list1:
    if entry[0] not in list2_keys:
        newlist.append(entry)

1 Comment

The big difference between this and the previous answer is that this compares only the first elements of the tuples. The other answer compares the whole tuple.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.