I try to call up a list stored in the dictionary:
obj = {
'a' : [0,1,2],
'b' : {
'c' : [0,1,2]
}
}
The code below is correct for JavaScript, but not in Python
print(obj.a[0], obj.b.c[0])
Traceback (most recent call last):
File "test.py", line 57, in <module>
print(obj.a[0], obj.b.c[0])
AttributeError: 'dict' object has no attribute 'a'
So, How to get the items of the list?
First, don’t use
objectas a variable name.Then, the correct syntax for dictionary access is
if you want the first item of the list. If you want the entire list, use