Reputation: 818
I have built a 2D list (I still want to call it an array) to hold information about currency tokens as such:
Token = [["P",0,.01,"Penny"],["N",0,.05,"Nickel"],["D",0,.10,"Dime"],["Q",0,.25,"Quarter"]]
When I try to read the value of a token using this code:
for i in Token:
print (Token[i][3])
I am given an error:
TypeError: list indices must be integers or slices, not list
I'm not sure I understand the error, and have not had much success searching online for a solution. Any help you can offer would be greatly appreciated.
If it helps, code for the whole project is on GitHub.
Upvotes: 1
Views: 1801
Reputation: 71451
You are using a for-each loop which, in Python, will give you the item in the list, not an index. Also, you are using a set of sets, which is not valid syntax. Therefore, keep a list of list and just use one index:
Token = [["P",0,.01,"Penny"],["N",0,.05,"Nickel"],["D",0,.10,"Dime"],["Q",0,.25,"Quarter"]]
for i in Token:
print(i[3])
Upvotes: 6
Reputation: 5193
For each iteration through Token you're getting that item as i
. So on your first iteration:
for i in Token:
i = ["P",0,.01,"Penny"]
Just do:
for i in Token:
print i[3]
Upvotes: 2