Home > Enterprise >  Remove duplicates from a list and remove elements at same index in another list
Remove duplicates from a list and remove elements at same index in another list

Time:06-16

I have two lists, one with word and another one with word type like this

['fish', 'Robert', 'dog', 'ball', 'cat', 'dog', 'Robert']
['animal', 'person', 'animal', 'object', 'animal', 'animal', 'person']

I need to remove duplicates in the first list and to remove the type at the same index of the word removed.

In the end I need something like this:

['fish', 'Robert', 'dog', 'ball', 'cat']
['animal', 'person', 'animal', 'object', 'animal']

How can I do that?

CodePudding user response:

This would be much easier with dictionaries (see below)

With lists

# Inputs
list1 = ['fish', 'Robert', 'dog', 'ball', 'cat', 'dog', 'Robert']
list2 = ['animal', 'person', 'animal', 'object', 'animal', 'animal', 'person']

# New lists
new_list1 = []
new_list2 = []

# Loop through elements of input lists
for a, b in zip(list1, list2):
    # If it isn't a duplicate, append to the new lists
    if a not in new_list1:
        new_list1.append(a)
        new_list2.append(b)

# Output new lists
print(new_list1)
print(new_list2)

Output:

['fish', 'Robert', 'dog', 'ball', 'cat']
['animal', 'person', 'animal', 'object', 'animal']

With a dictionary

# This uses the fact that dictionaries have unique keys, so this cancels out all the duplicates
x = dict(zip(
    ['fish', 'Robert', 'dog', 'ball', 'cat', 'dog', 'Robert'],
    ['animal', 'person', 'animal', 'object', 'animal', 'animal', 'person']
))

# Output dictionary
print(x)

# Output keys (equivalent of new_list1)
print(list(x.keys()))

# Output values (equivalent of new_list2)
print(list(x.values()))

Output:

{'fish': 'animal', 'Robert': 'person', 'dog': 'animal', 'ball': 'object', 'cat': 'animal'}
['fish', 'Robert', 'dog', 'ball', 'cat']
['animal', 'person', 'animal', 'object', 'animal']

CodePudding user response:

just do a loop and use the zip function

Script

# Input Vars
a = ['fish', 'Robert', 'dog', 'ball', 'cat', 'dog', 'Robert']
b = ['animal', 'person', 'animal', 'object', 'animal', 'animal', 'person']

# Output Vars
c = []
d = []

# Process
for e, f in zip(a, b):
    if(e not in c):
        c.append(e)
        d.append(f)

# Print Output
print(c)
print(d)

Output

['fish', 'Robert', 'dog', 'ball', 'cat']
['animal', 'person', 'animal', 'object', 'animal']

CodePudding user response:

You can use zip two lists and use the idea of dictionary that keys can be repeated only one time then return keys and values as two lists that you want:

lst1 = ['fish', 'Robert', 'dog', 'ball', 'cat', 'dog', 'Robert']
lst2 = ['animal', 'person', 'animal', 'object', 'animal', 'animal', 'person']
res = dict(zip(lst1, lst2))
# res -> {'Robert': 'person','ball': 'object', 'cat': 'animal', 'dog': 'animal', 'fish': 'animal'}
list(res.keys())
# ['fish', 'Robert', 'dog', 'ball', 'cat']

list(res.values())
# ['fish', 'Robert', 'dog', 'ball', 'cat']

CodePudding user response:

You can use a set to de-duplicate and then use index to get only the first occurrence from the word_type list

word = ['fish', 'Robert', 'dog', 'ball', 'cat', 'dog', 'Robert']
word_type = ['animal', 'person', 'animal', 'object', 'animal', 'animal', 'person']
unique_word = list(set(word))
updated_word_type = [word_type[word.index(_)] for _ in unique_word]

Output

# unique_word
['dog', 'fish', 'Robert', 'ball', 'cat']

# updated_word_type
['animal', 'animal', 'person', 'object', 'animal']
  • Related