Search code examples
pythondictionarytuplesattributeerror

Multiple dictionaries causing AttributeError?


I have 1 variable that contains multiple dictionaries:

a = {"foo": "foo"}, {"foo2": "foo2"}

But if I do:

a.get("foo")

it returns as AttributeError:

AttributeError: 'tuple' object has no attribute 'get'

Solution

  • Multiple dictionaries does not exist in Python. If you define a as:

    a = {"foo": "foo"}, {"foo2": "foo2"}
    

    a will be a tuple. So you have to call the element as follow:

    a[0].get("foo")
    

    To use a.get method you have to define a as follow:

    a = {"foo": "foo", "foo2": "foo2"}
    

    Now a.get("foo") call will have as output "foo".