2

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.

David Greydanus
  • 2,551
  • 1
  • 23
  • 42
  • 3
    Please avoid using names like list, dict etc as they shadow the in-built functions – ersran9 Jul 05 '13 at 16:15
  • possible duplicate of [check if a number already exist in a list in python](http://stackoverflow.com/questions/14667578/check-if-a-number-already-exist-in-a-list-in-python) – w-m Jul 05 '13 at 16:21
  • Your question is vague. Tell us exactly what error you see, and provide code that reproduces the error (not code "something like" what causes the problem). – chepner Jul 05 '13 at 16:24
  • Possible duplicate of [Fastest way to check if a value exist in a list](http://stackoverflow.com/questions/7571635/fastest-way-to-check-if-a-value-exist-in-a-list) – David Greydanus Nov 01 '15 at 00:46

1 Answers1

10

Why not just use:

if a not in some_list:
    some_list.append(a)
Jon Clements
  • 138,671
  • 33
  • 247
  • 280