I'm trying to remove duplicate items from a multidimensional list. My goal is to remove items that are the same across the lists.
For example: List 2, List 6 and List 7 contains the car Bentley. I want to remove that car from 2 of the lists.
How do I accomplish this?
The code below only works if I pass in a single list containing duplicate entries, but I need to deduplicate a multidimensional list.
cars = [
["Acura", "Alfa Romeo", "Aston Martin", "Audi", "Aston Martin"],
["Bentley", "BMW", "Bugatti", "Buick"],
["Cadillac", "Chrysler", "Citroen"],
["Dodge", "Ferrari", "Fiat", "Ford"],
["Geely", "Honda", "Hyundai", "Infiniti"],
["Alfa Romeo", "Bentley", "Hyundai", "Lamborghini"],
["Koenigsegg", "Bentley", "Maserati", "Lamborghini"]
]
def remove(duplicate):
final_list = []
for num in duplicate:
if num not in final_list:
final_list.append(num)
return final_list
print (remove(cars))
returns:
[
['Acura', 'Alfa Romeo', 'Aston Martin', 'Audi','Aston Martin']
['Bentley', 'BMW', 'Bugatti', 'Buick'],
['Cadillac', 'Chrysler', 'Citroen'],
['Dodge', 'Ferrari', 'Fiat', 'Ford'],
['Geely', 'Honda', 'Hyundai', 'Infiniti'],
['Alfa Romeo', 'Bentley', 'Hyundai', 'Lamborghini'
['Koenigsegg', 'Bentley', 'Maserati', 'Lamborghini']
]
My desired output after deduplication is shown below. No list within this multidimensional list contains a duplicate entry.
[
['Acura', 'Alfa Romeo', 'Aston Martin', 'Audi']
['Bentley', 'BMW', 'Bugatti', 'Buick'],
['Cadillac', 'Chrysler', 'Citroen'],
['Dodge', 'Ferrari', 'Fiat', 'Ford'],
['Geely', 'Honda', 'Hyundai', 'Infiniti'],
['Bentley', 'Hyundai', 'Lamborghini'
['Koenigsegg', 'Maserati']
]