How to remove a tuple froma list of tuples

Hi,
In the next snippet of code I want to remove a tuple from a list of tuples:
¨¨
for lid <- kopie_leden , contr_lid <- contributie,
do: {IO.inspect(lid.relatiecode), IO.inspect(contr_lid.relatiecode),
case lid.relatiecode == contr_lid.relatiecode do
true -> {IO.puts("--------> Equal \n"), remove_lid_from(leden, [lid]) }
false -> {add_leden([], [lid]), IO.puts(“Not equal \n”)}
end
} ¨¨
I want a function to remove a tuple. Any suggestions are welcome.

Thiel

If all the keys in your list are atoms, you can treat it as a Keyword list.
Then, you can use Keyword.delete/2 or Keyword.delete_first/2 depending on what you want to achieve. If not, I guess you could just use Enum.reject/2 to remove the values you want.

3 Likes

Hi,
I tried your suggestion but it did not work with Enum.reject. Because I wanted the complement of two sets I wrote a simple program:

 def find( relatiecode, [head | tail], found) do 
     case (relatiecode == head.relatiecode) do
     true  -> find(relatiecode, [],true)  
     false -> find(relatiecode, tail, false)  
   end
 end
 
 def find(_relatiecode, [], found), do: found

 def complement([head|tail], contr, acc) do  
     relatiecode = head.relatiecode
     found = false

  case find(relatiecode, contr, found) do  
     true  -> complement(tail, contr, acc)
     false -> acc = acc ++ [head]
     complement(tail, contr, acc)    
   end
 end
 
 def complement([], _contr, acc) , do: acc

But anyhow thanks for your anser.
Best regards,
Thiel

How is this module supposed to be used?

1 Like

There’s List.keydelete for any list of tuples.

@Thiel Could you explain a little better what you’re trying to achieve?