1

I want to remove duplicated in a python list in a way that allows me to alter another corresponding list in the same way. In the below example original is the list I want to de-duplicate. Each element in key that shares the same index as original corresponds to each other:

original = [a,a,a,3,4,5,b,2,b]
key = [h,f,g,5,e,6,u,z,t]

So I want to remove duplicates in original such that whatever element I delete from original I delete the corresponding element (of the same index) in key. Results I want:

deduplicated_original = [a,3,4,5,b,2]
deduplicated_key = [h,5,e,6,u,z]

I can get deduplicated_original using list(set(original)) however I cannot get the corresponding deduplicated_key

asked Sep 22, 2017 at 13:59
2
  • Is ['g', 5, 'e', 6, 't', 'z'] okay? removal of all but the last? Commented Sep 22, 2017 at 14:03
  • Can you please show any attempts you've made? Where exactly are you stuck? Commented Sep 22, 2017 at 14:04

3 Answers 3

6

You can use a set to keep track of duplicates and enumerate() to iterate over indexes/values of the original list:

seen = set()
lst = []
for i, v in enumerate(original):
 if not v in seen:
 lst.append(key[i])
 seen.add(v)
print(lst)
answered Sep 22, 2017 at 14:05
1

maybe less elegant, less easy to follow the list revesal, index slicing

the inner list comp walks the input list org backwards, asking if there is a prior matching element, if so record the index of this duplicate

[len(org) - 1 - i
 for i, e in enumerate(org[::-1]) if e in org[:-i-1]]

then the outer list comp uses .pop() to modify org, ky as a side effect

nested list comprehension 'dups', a 'one liner' (with line breaks):

org = ['a','a','a',3,4,5,'b',2,'b']
ky = ['h','f','g',5,'e',6,'u','z','t']
dups = [(org.pop(di), ky.pop(di))
 for di in [len(org) - 1 - i
 for i, e in enumerate(org[::-1]) if e in org[:-i-1]]]
org, ky, dups
Out[208]: 
(['a', 3, 4, 5, 'b', 2],
 ['h', 5, 'e', 6, 'u', 'z'],
 [('b', 't'), ('a', 'g'), ('a', 'f')]) 

of course you don't actually have to assign the list comp result to anything to get the side effect of modifying the lists

answered Sep 22, 2017 at 15:47
0

You can manually get all the indices of duplicates like this:

indices = []
existing = set()
for i, item in enumerate(original):
 if item in existing:
 indices.append(i)
 else:
 existing.add(item)

and then remove those indices from your key list, in reverse because deleting a key changes the indices of further items:

for i in reversed(indices):
 del key[i]
answered Sep 22, 2017 at 14:05

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.