0

I have a list

[[1, -2, 3], [1, -2, -4], [1, -2, 1], [3, -2, 1], [-4, 1, 2]]

and I want to keep single copy of the sub lists with exact same elements. i.e remove

[[3, -2, 1], [-4, 1, 2]]

and get following list.

[[1, -2, 3], [1, -2, -4], [1, -2, 1]]

What could be the fastest way of doing this?

I am newbie, so language may be bad.

wim
  • 338,267
  • 99
  • 616
  • 750
Lucky M.E.
  • 91
  • 1
  • 4

3 Answers3

-1

If you want a solution that works for all lists, not just the one you provided, you can use set and frozenset to get a set of unique frozensets and convert back to lists.

l = [[1, -2, 3], [1, -2, -4], [1, -2, 1], [3, -2, 1], [-4, 1, 2]]
unique = set(map(frozenset, l))

will give you a set of frozen sets. If you want to convert the elements back into lists you can do

unique_l = list(map(list, set(map(frozenset, l))))

per @wim's points if the sub-lists can contain duplicates

kept = {}
for e in l:
    key = tuple(sorted(e))
    if key not in kept:
        kept[key] = e
unique_l = list(kept.values())

and if order of the original list and the sublists matter as well you wanting to be keep the first occurrence of the unique sublist.

modesitt
  • 7,052
  • 2
  • 34
  • 64
  • Frozenset will fail if there are any duplicate entries in the sublists. – wim Mar 19 '20 at 21:19
  • No, I don't think you got my point. Consider `[[1,1,2], [1,2,2]]`. – wim Mar 19 '20 at 21:25
  • so the answers given in the question that you marked as closing this one - including yours - don't work either... because the question is different – modesitt Mar 19 '20 at 21:38
  • How so - in what way does the [second answer](https://stackoverflow.com/a/50769590/674039) not work? – wim Mar 19 '20 at 21:43
  • 1
    Only if OP does not care about 1. the original order of the sub lists as your constructor will take the later and 2. if to your point that `[[1, 1, 2], [1, 2, 2]]` should give back `[[1, 1, 2], [1, 2, 2]]` and not just `[[1,1,2]]` which I think should be the case. – modesitt Mar 19 '20 at 21:46
  • If you wanted the first element returned rather than last element, it's easy to adapt- you just iterate the input using `reversed`. – wim Mar 19 '20 at 21:48
  • I dont think as values order does not update in dict. It would give `[[1, -2, -4], [1, -2, 1], [1, -2, 3], [-4, 1, 2]]` if `list((ist(OrderedDict((frozenset(edge), edge) for edge in reversed(l)).values()))` - no? (and not reversing the final result is also wrong - correct? Also does not solve number (2) – modesitt Mar 19 '20 at 21:51
  • 1
    I see where you're coming from. But it's not clear from question whether your interpretation or my interpretation is the right one, so I'm waiting for clarification from O.P. on that – wim Mar 19 '20 at 21:56
  • that is fair @wim – modesitt Mar 19 '20 at 21:58
-2

if I understand this question very well you want to remove [[3, -2, 1], [-4, 1, 2]] from the main list to get [[1, -2, 3], [1, -2, -4], [1, -2, 1]] and still retain the original list you can simply use this code to copy a list.

main_list = [[1, -2, 3], [1, -2, -4], [1, -2, 1], [3, -2, 1], [-4, 1, 2]]
copy_list = main_list[:]
print(copy_list[:-2]) # this is backward indexing. or
print(copy_list[:3]) # This is forward indexing.
print(main_list) # checks if the main list is still intact.....
-4

A quick way to return list without a specific element in Python

Example

list_1 = [[1, -2, 3], [1, -2, -4], [1, -2, 1], [3, -2, 1], [-4, 1, 2]]

new_list = list(list_1)

new_list.remove([3, -2, 1], [-4, 1, 2])

if you want to remove by index use the .pop() command

https://note.nkmk.me/en/python-list-clear-pop-remove-del/

cg4tw
  • 5
  • 5