20

I have a class:

class Car:
    make
    model
    year

I have a list of Cars and want to get a list of unique models among my Cars.

The list is potentially tens of thousands of items. What's the best way to do this?

Thanks.

Steven
  • 1,049
  • 2
  • 14
  • 32

4 Answers4

50

Use a set comprehension. Sets are unordered collections of unique elements, meaning that any duplicates will be removed.

cars = [...] # A list of Car objects.

models = {car.model for car in cars}

This will iterate over your list cars and add the each car.model value at most once, meaning it will be a unique collection.

Ffisegydd
  • 51,807
  • 15
  • 147
  • 125
  • 11
    Many people find the curlies to read - they look like dictionaries. In that context, so as an alternative `models = list(set([car.model for car in cars]))` – theodox Sep 25 '14 at 16:40
  • 8
    you pay quite some price for getting rid of the curlies, don't you? – flaschbier Sep 25 '14 at 18:55
  • 1
    Yeah I think it's because first you create the full list (with duplicates) then you add it to a set (meaning you remove the duplicates) then you put it back in a list. – Ffisegydd Sep 25 '14 at 19:02
  • 3
    Feed your clock cycles to the god of the anti-curlie... – Tatarize Mar 01 '21 at 08:45
2

Add a method to the class that returns a unique list

def unique(list1):
    # intilize a null list
    unique_list = []

    # traverse for all elements
    for x in list1:
        # check if exists in unique_list or not
        if x not in unique_list:
            unique_list.append(x)

    return unique_list

Otherwise,

If you're looking to pass a dictionary of make, model year then you can use pandas dataframe in order to get the unique values.

S Habeeb Ullah
  • 968
  • 10
  • 15
1

If you want to find cars that only appear once:

from collections import Counter
car_list = ["ford","toyota","toyota","honda"]
c = Counter(car_list)
cars = [model for model in c if c[model] == 1 ]
print cars
['honda', 'ford']
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
  • Yeah I read it differently (as the OP wanting the unique set of models that he had). Hopefully they'll reply and clear it up though :) – Ffisegydd Sep 25 '14 at 16:32
  • +1: either this is the answer the OP is looking for, or this is just a duplicate of 50 other questions. – tom10 Sep 25 '14 at 16:45
-4

Now it depends on what you mean by unique. If you mean Exactly the same object then this should work:

def check_all_objects_unique(objects: List[Any]) -> bool:
unique_objects = set(objects)
if len(unique_objects) == len(objects):
    return True
elif len(unique_objects) < len(objects):
    return False

If by unique you mean objects with the same attribute values then other answers on here will help you.

henryJack
  • 4,220
  • 2
  • 20
  • 24
  • You don't answer OPs question. This just checks if all elements are unique and in a very bad way at that. simply `return len(unique_objects) == len(objects)` – R Tsch Jun 25 '19 at 16:10