0

Why am i getting this error?

i am trying to access a list within a list so i do somthing like this: Lst = [[O,O],[O,O],[O,O]] Lst[2][1] Then it tells me that this: Lst[2][1] is invalid syntax i dont understand...

23rd Oct 2016, 2:50 AM
ZenoTheWiz
7 Answers
+ 2
i tried your code. at first, i got an error sayin "O is not defined" so i changed all the numbers. then again after using print(), i got the output. so use this code:: Lst=[[0,0],[0,0],[0,0]] print(Lst[2][1])
23rd Oct 2016, 5:43 AM
Parth Krishna
Parth Krishna - avatar
+ 1
if you think about it, you'll see that arrays are variables or memory spaces. just like any other variable, you can't just call the variable to do nothing. it like having int a = 2 on one line and then on the next line have just a. the program will ask you, "what the heck should I do with a?" that's why @parth's program didn't complain cause of using the array with a print statement
23rd Oct 2016, 7:28 AM
Mutegeki Ronald James
Mutegeki Ronald James - avatar
+ 1
if you think about it, you'll see that arrays are variables or memory spaces. just like any other variable, you can't just call the variable to do nothing. it like having int a = 2 on one line and then on the next line have just a. the program will ask you, "what the heck should I do with a?" that's why @parth's program didn't complain cause of using the array with a print statement
23rd Oct 2016, 7:28 AM
Mutegeki Ronald James
Mutegeki Ronald James - avatar
0
Lst[2][1] is not valid as you are only traversing the value but not showing it anywhere or using it somewhere else Try printing the value and it will work!
23rd Oct 2016, 11:32 AM
ARPIT GUPTA
ARPIT GUPTA - avatar
0
In addition to what Parth Krishna said: the items you are using in the list are characters, which should be specified like this: Lst=[['0','0'],['0','0'],['0','0']]. If you don't do this, you will still have an error: Lst = [[O,O],[O,O],[O,O]] NameError: name 'O' is not defined
25th Oct 2016, 11:57 AM
Ticana Daniel
Ticana Daniel - avatar
0
that's just one of many Python bugs. If you want access a list within a list you need one more variable: new_lst = Lst[2] print new_lst[1]
12th Nov 2016, 9:19 PM
Davor
0
that's just one of many Python bugs. If you want access a list within a list you need one more variable: new_lst = Lst[2] print new_lst[1]
12th Nov 2016, 9:19 PM
Davor