Match element then remove both elements

Question:

My list consists on several items:

["book","rule","eraser","clipboard","pencil",etc]

Let say I have an element ["book"] and I want to match with another element ["rule"] with the same len (4). then I want to remove both elements from the list.

["eraser","clipboard","pencil",etc]

I tried using a for loop and zip(lists, lists[1:]) but I can only remove the elements next to each other while other elements (like papers and pencil, len = 6) are never removed.

I want to remove same length words and have a final list with only the words whose length has no match or found no pair. For example:

["book","rule","eraser","clipboard","pencil","book"]

then the final list will be:

["clipboard","book"]

as book and clipboard found no pair.

Asked By: Dude Rar

||

Answers:

This answer was based on misunderstanding the question. I tried to delete it, but it’s not working, so I’ve edited out the text.

Answered By: Barmar

You can try:

from collections import Counter

lst = ["book", "rule", "eraser", "clipboard", "pencil", "book"]

c = {k: v % 2 for k, v in Counter(map(len, lst)).items()}

out = []
for v in reversed(lst):
    if c[len(v)] == 1:
        out.append(v)
    c[len(v)] -= 1


print(out[::-1])

Prints:

['clipboard', 'book']
Answered By: Andrej Kesely

Try this:

list1 = ["book","rule","eraser","clipboard","pencil", 'book']

# itterate through list1
for item1 in list1:
   # create a copy of list1 without the current item
   list2 = list1.copy()
   list2.remove(item1)
   # itterate through list2 and compare the length of the current item to the current item in list1
   for item2 in list2:
      if len(item1) == len(item2):
          list1.remove(item1)
          list1.remove(item2)
          list2.remove(item2)
          break
    
print(list1)
Answered By: parvez alam

Much simpler, think of it as turning on or off a flag. If the flag is on, you turn it off by deleting. If it’s off, you turn it on by adding the word. Flags are based on same length:

lst = ["book", "rule", "eraser", "clipboard", "pencil", "book"]
output = {}
for item in lst:
    length = len(item)
    popped = output.pop(length, None)
    if popped is None:
        output[length] = item
print(list(output.values()))
Answered By: Bharel
input_list = ["book", "rule", "eraser", "clipboard", "pencil", "book"]
output_list=[]

#Each entry in list
for i,val_i in enumerate(input_list):
    len_match=False
    pair_match=False
    #Compared to each entry in the list
    for j,val_j in enumerate(input_list):
        #same index skip
        if i ==j:
            continue
        #found value match
        if val_i==val_j:
            pair_match= True
            break
        #found same length
        if len(val_i) == len(val_j):
            len_match = True
            break
    
    #if found pair or no length match add to output list
    if pair_match or (not len_match):
        output_list.append(val_i)

print(output_list)

output

['clipboard', 'book']
Answered By: KRG
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.