Super easy question. Let´s say I have this list in Python:
variables = ['A1,A1','A2,B2','A1,C2','B3,B3','C4,C4']
Now, I only need to keep those items where the value before and after the comma differs. In this case, the output would be:
result = ['A2,B2','A1,C2']
I already have a 'not-so-elegant' solution for this:
new_list = []
for i in range(len(variables)):
j = variables[i].split(",")
if j[0] != j[1].replace(" ", ""):
z = "{},{}".format(j[0], j[1])
new_list.append(z)
note: I had to add replace to remove whitespaces, but it´s not important...
Is there another (better) way to do this? Maybe regex?
note II: I also tried using a list comprehension:
lista_differents = ["{},{}".format(j[0], j[1]) for i in range(len(variables)) if j[0] != j[1].replace(" ", "")]
But I still have to figure out how to add the line j = variables[i].split(",")
Any ideas?