0

I have an python object list. for example "all_names" I can retrieve values from that list in this way:

for a in all_names:
    print a.name,a.age

My target is to quickly find out if a certain name exist in the list all_names.

so I'm doing it in this way.

all = []
for a in all_names:
    all.append(a.name)
if "any_name" in all:
    print "Name exit"
else:
    print "Not found"

But if size of "all_names" is very huge (ex. if len(all_names) > 100,000), then I think It will not be an efficient way to do the same.

So wanted to know, if is there any more efficient way than this??

Thanks in Adv.

3
  • Is storing all of the names in a global list as you're creating the objects out of the picture? Commented Aug 18, 2013 at 17:47
  • all_names is a really bad variable name for a list of things which are not names Commented Aug 18, 2013 at 18:05
  • 1
    although all is an even worse variable name, since it shadows the builtin all Commented Aug 18, 2013 at 18:09

4 Answers 4

1

Try:

if any(i.name == "any_name" for i in all_names):
    print "Name exists"
else:
    print "Not found"

If you need to find a name only once, this is the way. If you need to find more names - use set:

names = set(i.name for i in all_names)
if "any_name" in names:
    print "Name exists"
else:
    print "Not found"
Sign up to request clarification or add additional context in comments.

5 Comments

@AnttiHaapala, yes set is faster, but take into account time and memory for its construction.
@AnttiHaapala: I beleive the original was actually O(N^2), due to the dynamic reallocation of the list. This allocates constant memory.
No, the dynamic reallocation of the list is O(N). Anyway I removed my comment since you made a set version.
@AnttiHaapala: Then why is string concatenation (instead of ''.join) often described as having quadratic complexity?
Yes, string concatenation has quadratic complexity, and so does tuple concatenation, and list concatenation (a + b), but list append + extend has linear complexity.
0

create all_name as
all_name={a.name for a in all_names}
rest everything is fine in your code.. .


Note:don't use reserved built-ins(in your case all) names

3 Comments

Please don't use the builtin all as a variable name
thanks Eric for pointing out that. . i have just forgotten that user has used that keyword. .
all is not a keyword. I hadn't noticed that the OP also made this mistake
0

Use a set.

all_set = set(a.name for a in all_names)

if "any_name" in all_set:
    print "Name exists"
else:
    print "Not found"

The set works like a dictionary, with O(1) access time. Of course to appreciate the speed of the set, you need to reuse it (ask for several names on the same set).

Comments

0

What about this:

for i in all_names:
    if i.name == "any_name":
        print("Name found!")
        break
else:
    # The loop wasn't broken out of
    print("Name not found")

Alternatively:

if any(i.name == "any_name" for i in all_names):
    print("Name found!")
else:
    print("Name not found")

2 Comments

this is only 50 % faster than his original approach
Using a set would require much more memory, although I'd agree it would be a better approach if it was reused.

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.