1

This code gave me an error, how can I check if a variable is in a list?

TomMarks = [66,54,34,79]
JackMarks = [66,54,34,79]
myList = [TomMarks, JackMarks]

if KateMarks in myList:
    print("yes")
else:
    print("no")
Cœur
  • 37,241
  • 25
  • 195
  • 267
Dennisboys
  • 583
  • 3
  • 9
  • 22
  • 4
    `KateMarks` isn't defined (at least in the code you give us). Also, don't use `list` as a variable name - it will overwrite the built-in `list` function. – Volatility Apr 15 '13 at 02:57
  • Hi Volatility, yes, the KateMarks is undefined on purpose, I just want to know if there is a way to check if an undefinied variable is in a list? I expect the if statement yields a "no" print in the above case. Thanks. – Dennisboys Apr 15 '13 at 03:00
  • 1
    Are you sure you weren't looking for a `dict` where you can check for the existence of a key? This feels like an XY problem. – FatalError Apr 15 '13 at 03:02
  • 2
    You can't have a program with undefined variable, mate. It works perfectly fine if you define it. – sashkello Apr 15 '13 at 03:02
  • It's a good question if formulated in a proper way... But was asked before http://stackoverflow.com/questions/843277/how-do-i-check-if-a-variable-exists-in-python – sashkello Apr 15 '13 at 03:16

7 Answers7

3

Python can't check if something is in a list if it doesn't know what that something is in the first place. (In general, you get an error if you try to reference a variable that hasn't been defined yet.)

In your example, even though you define myList = [TomMarks, JackMarks], if you print the list, you get this:

[[66, 54, 34, 79], [66, 54, 34, 79]]

If you define KateMarks beforehand, then yes the code will run perfectly. But if the variable is undefined, the Python won't know what to check for.

Volatility
  • 31,232
  • 10
  • 80
  • 89
3

You need to define a variable before using it. You are telling compiler "KateMarks" and it doesn't know what it is - how do you suppose it would go about it?

This:

TomMarks = [66,54,34,79]
JackMarks = [66,54,34,79]
myList = [TomMarks, JackMarks]

KateMarks = 1

if KateMarks in myList:
    print("yes")
else:
    print("no")

works fine and prints "no".

What you probably want is to check if the variable exists first:

if 'KateMarks' in locals():
sashkello
  • 17,306
  • 24
  • 81
  • 109
1

If your really want to print no even if KateMarks is not defined, you can do the following, but it is not a good way of going about it.

TomMarks = [66,54,34,79]
JackMarks = [66,54,34,79]
myList = [TomMarks, JackMarks]

try:
    if KateMarks in myList:
        print 'yes'
    else:
        print 'no'
except(NameError):
    print 'no'
Akavall
  • 82,592
  • 51
  • 207
  • 251
1

Python variable names are references to objects. Because KateMarks is not referencing anything Python will raise a NameError exception.

Namespace introspection can be acheived with locals() or dir()

'KateMarks' in dir()
alexhb
  • 435
  • 2
  • 12
1

Instead of lists you can do it by dictionaries, like this:

myList = {'TomMarks':[66,54,34,79], 'JackMarks':[66,54,34,79]}

if 'KateMarks' in myList:
    print("yes")
else:
    print("no")
CodeArtist
  • 5,534
  • 8
  • 40
  • 65
0

Do you mean you're writing some sort of code where you'll get Kate's marks somewhere else in the program, and then check if the marks are in it?

Or actually, on second thought I think the whole list-in-a-list and marks idea is just a distraction... you really mean to ask if you can use a variable name in a similar fashion to a string, right? If that's the case, there's a somewhat not-so-efficient way to do what I think you're trying to do, using dict:

marks = {} # a Python dictionary
marks["Tom"] = 1234123412512352345
marks["Jack"] = 2394872394857389578935

if "Kate" in marks:
    print("Kate is a key in marks.")
else:
    print("Kate isn't a key, but at least now we know that we didn't get Kate's marks.")
SimonT
  • 2,219
  • 1
  • 18
  • 32
0

If variable is undefined you just simply check if it is undefined. No undefined variables can be in any list:

if not 'undefined_variable' in dir():
   print "NO WAY!"
nad2000
  • 4,526
  • 1
  • 30
  • 25