0

I'd like to remove duplicates from several lists (in-place). Have tried the following but failed (Python Compiler):

a = [1, 2, 2]
b = [3, 4, 4]
c = [5, 6, 6]

print('===previous')
print(id(a))
print(id(b))
print(id(c))
print('---previous')

for l in (a, b, c):
  l = list(dict.fromkeys(l))
  print('===middle')
  print(id(l))
  print('---middle')

print('===after')
print(id(a))
print(id(b))
print(id(c))
print(a)
print(b)
print(c)
print('---after')

I know this is because (Python Variable)

Variables (names) are just references to individual objects.

Just want to ask if any efficient way can achieve this goal, thank you.

Che-Hao Kang
  • 133
  • 1
  • 7
  • Are you trying to remove duplicates in the individual lists? So your desired output would be ```a = [1, 2], b = [3, 4], c = [5, 6]``` ? – goalie1998 Jan 09 '21 at 04:42
  • `l = list(dict.fromkeys(l))` That creates a _new_ list. You're not actually modifying the original lists. – John Gordon Jan 09 '21 at 04:43
  • @goalie1998 Yes, that's correct. – Che-Hao Kang Jan 09 '21 at 04:45
  • @JohnGordon Yes, I know ```l = list(dict.fromkeys(l))``` doesn't modify the original lists. Just want to if any way can efficiently apply the same action to several lists in-place. Thanks. – Che-Hao Kang Jan 09 '21 at 04:46

3 Answers3

1

Use sets. Duplicates are not allowed in sets:

a = set(a)
b = set(b)
c = set(c)

If you need them to be in a list again:

a = list(set(a))
b = list(set(b))
c = list(set(c))
goalie1998
  • 1,427
  • 1
  • 9
  • 16
  • Thanks. Is it possible to use for-loop to modify all lists, like: ```for l in (a, b, c): l = set(l)``` . I know the above doesn't work. Just want to know if any way is able achieve this. – Che-Hao Kang Jan 09 '21 at 04:48
1
a = [1, 2, 2]
b = [3, 4, 4]
c = [5, 6, 6]


a=list(dict.fromkeys(a))
b=list(dict.fromkeys(b))
c=list(dict.fromkeys(c))


print(a)
print(b)
print(c)
Ted Klein Bergman
  • 9,146
  • 4
  • 29
  • 50
Mohammed Jubayer
  • 1,709
  • 1
  • 11
  • 5
1

You have to override the value and not the variable name. If you need to modify a dynamic amount of lists, then you can override all values in the list by taking a full slice of each list:

for my_list in (a, b, c):
     my_list[:] = set(my_list)
Ted Klein Bergman
  • 9,146
  • 4
  • 29
  • 50