David Greydanus
David Greydanus

Reputation: 2567

How can I search through a list in python for something that might not exist?

For example something like:

if l.index(a)== -1:
    l += [a]

If I run something like this, I will get a value error.I am assuming this is not a new problem.

Upvotes: 2

Views: 109

Answers (1)

Jon Clements
Jon Clements

Reputation: 142166

Why not just use:

if a not in some_list:
    some_list.append(a)

Upvotes: 10

Related Questions