0

I have a nested list like this:

my_list = [['10', 'A', 'A'], ['5', 'B', 'A'], ['2', 'B', 'B'], ['10','A','B']]

I would like to check if there is a duplicate letter in the last two positions and if there is not, print those lists.

final_list = [['5','B','A'],['10','A','B']

Finally, I would like to print the numerical value from each of these lists:

only_numbers = ['5','10']

However, I'm getting stuck at the identifying duplicates within the lists. I have found this answer Removing Duplicates from Nested List Based on First 2 Elements, but I when I tried to apply this on my example code above (and actual code), I got some lists with duplicates and some without duplicates.

seen = set() 
seen_add = seen.add
final_list = [x for x in my_list if tuple(x[-2:]) not in seen and not seen_add(tuple(x[-2:]))]

But I get:

final_list = [['10', 'A', 'A'], ['5', 'B', 'A'], ['2', 'B', 'B'], ['10', 'A', 'B']]

What am I missing?


EDIT: Fixed my personal coding attempt below so that its functions (thank you to Karl Knechtel for your explanation)

only_numbers = []
for x in my_list:  
     if not x[1] == x[2]:
         add_number.append(x[0])
print(only_numbers)
  • 1
    "I have found this answer Removing Duplicates from Nested List Based on First 2 Elements" When you say "duplicate", you mean something completely different from the other person. You want to check two different elements *within each sublist*; the other code is about comparing of elements *between different sublists*. In your example data, you have one sublist each with a different pair of letters - AA, AB, BA, and BB - none of those is equal to any other, so no duplicates are detected. – Karl Knechtel Sep 22 '20 at 03:54
  • 1
    In general, the way you do list comprehensions that care about *each element in isolation* is: first you come up with a *rule that tells you what to do with the element*, then you write the comprehension so it applies the rule to every element. In your case, the rule is to take that element (which is also a list) and compare the last two values in *that* list. So, write the code that does the comparison first. – Karl Knechtel Sep 22 '20 at 03:56
  • 1
    "And if someone has the time, please explain why my logic for this code below I wrote is wrong" The way in which it is wrong is that you get three copies of each number. The reason for this is that the `i` loop is unwanted: you only want to `.append` the selected `x[0]` value once, but you append it every time through the inner `i` loop. It loops three times because your sublists have 3 elements in them. – Karl Knechtel Sep 22 '20 at 03:59
  • 1
    Perhaps you automatically wrote a nested loop because you have nested lists. The issue is that you do not treat the inner lists as something to loop over; you have a specific way of looking at specific indexes within the inner list. – Karl Knechtel Sep 22 '20 at 04:00

1 Answers1

2

This is one approach using filter and a list comprehension

Ex:

my_list = [['10', 'A', 'A'], ['5', 'B', 'A'], ['2', 'B', 'B'], ['10','A','B']]
result = [i for i, *_ in filter(lambda x: x[-1] != x[-2], my_list)]
# OR 
# [i for i, j, k in my_list if j != k]
print(result)

Output:

['5', '10']
Rakesh
  • 81,458
  • 17
  • 76
  • 113