S
S
Shandy2021-11-14 19:28:18
Python
Shandy, 2021-11-14 19:28:18

How to quickly find an object in the list?

Now it works like this, but it will be very slow when there are more servers

for server in self.servers:
    if server.id == id:
        return server

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
javedimka, 2021-11-14
@MrShandy

If your list does not have a billion servers, then no, it will not be slow.
But if you really want to, then a typical tactic is to make a lookup_field: object dictionary.
If you absolutely need lists, you can use the bisect module.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question