Most efficient way to find objetcs in list of objects

San*_*o J 0 python arrays list

I have a list or array (what is the correct term in python?) of objects. What is the most efficient way to get all objects matching a condition?

I could iterate over the list and check each element, but that doesn't seem very efficient.

objects = []
for object in list:
  if object.value == "123":
    objects.add(object)
Run Code Online (Sandbox Code Playgroud)

geo*_*org 6

This is the simplest way:

objects = [x for x in someList if x.value == 123]
Run Code Online (Sandbox Code Playgroud)

If you're looking for a faster solution, you have to tell us more about your objects and how the source list is built. For example, if the property in question is unique among the objects, you can have better performance using dict instead of list. Another option is to keep the list sorted and use bisect instead of the linear search. Do note however, that these optimization efforts start making sense when the list grows really big, if you have less than ~500 elements, just use a comprehension and stop worrying.