-4

I need to remove "duplicates" from this list:

[[4, 1], [1, 4], [0, 5], [5, 0]]

For example: [4, 1] [1, 4] are the same object, and I need to remove one of them.

How do I do that without using list comprehension tools?

Malekai
  • 4,765
  • 5
  • 25
  • 60
trizz
  • 57
  • 6

3 Answers3

1

One method is to sort it and append if not present in the final list like mentioned by LogicalBranch in the answers.

You mentioned you cant use sort and always there are 2 elements in the list. Then you can do a simple trick by making another list which is reverse of the list and comparing it in the final answer. See the codes below

ans = []
l = [[4, 1], [1, 4], [0, 5], [5, 0]]
for x in l:
    a = x[::-1]
    if x not in ans and a not in ans:
        ans.append(x)

print(ans) # [[4, 1], [0, 5]]
Sreeram TP
  • 11,346
  • 7
  • 54
  • 108
1

Based on the comments, you don't want to use list comprehension, sort and you always have 2 elements in sublist then following approach would help,

It iterates over the list and reverse the sublist and check if they present in the new_list

x = [[4, 1], [1, 4], [0, 5], [5, 0]]

new_list = []
for i in x:
    if i[::-1] not in new_list and i not in new_list:
        new_list.append(i)

print(new_list)

Output:

[[4, 1], [0, 5]]
Sociopath
  • 13,068
  • 19
  • 47
  • 75
0

The simplest way to achieve this (without importing anything) is by sorting each pair in the list before appending it to a new result list, like this:

result = []
for pair in [[4, 1], [1, 4], [0, 5], [5, 0]]:
  pair.sort()
  if pair not in result:
    result.append(pair)
print(result)

You could even convert this into a function:

def list_filter(collection):
  result = []
  for pair in collection:
    pair.sort()
    if pair not in result:
      result.append(pair)
  return result

Which you would then use like this:

list_filter([[4, 1], [1, 4], [0, 5], [5, 0]])

Which should return a list that looks like this:

[[1, 4], [0, 5]]

You could make this even smaller using:

list_filter = lambda collection: list(set([sorted(x) for x in collection]))

Which should return the same result.

EDIT: updated method without sort:

(result, collection) = ([], [[4, 1], [1, 4], [0, 5], [5, 0]])

def check(n1, n2):
  for pair in collection:
    if n1 in pair and n2 in pair and sorted(pair) in collection:
      return True
  return False

for pair in collection:
  pair.sort()
  if pair not in result:
    result.append(pair)

print(result)

You could even convert this into a function:

def new_filter_function(collection):
  result = []

  def check(n1, n2):
    for pair in collection:
      if n1 in pair and n2 in pair and ([n1, n2] in collection or [n2, n1] in collection):
        return True
    return False

  for pair in collection:
    if pair not in result:
      result.append(pair)

  return result

Which you would then use like this:

new_filter_function([[4, 1], [1, 4], [0, 5], [5, 0]])

Which should also return a list that looks like this:

[[1, 4], [0, 5]]

Good luck.

Malekai
  • 4,765
  • 5
  • 25
  • 60