24

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.

4
  • do you mean models that are only in the list once or to shorten the list to only have one of each? Commented Sep 25, 2014 at 16:24
  • build a set, i suppose? Commented Sep 25, 2014 at 16:24
  • Also: stackoverflow.com/questions/6926928/… stackoverflow.com/questions/479897/… Commented Sep 25, 2014 at 16:28
  • I am not sure this is a duplicate, the OP may want to find models that only appear once which the dup will not solve Commented Sep 25, 2014 at 16:30

4 Answers 4

55

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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]))
you pay quite some price for getting rid of the curlies, don't you?
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.
Feed your clock cycles to the god of the anti-curlie...
3

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.

Comments

2

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']

2 Comments

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 :)
+1: either this is the answer the OP is looking for, or this is just a duplicate of 50 other questions.
-5

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.

1 Comment

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)

Your Answer

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.