# Unable to comprehend the output generated

a = { }
b = { }
a[’ ‘] = b
b[’ '] = a

print b
output: {’ ‘: {’ ': {…}}}

can you please explain me how this works – i mean the output generated.

a= { }
b= { }
a[‘b’] = b
b[‘a’] = a

print b
{’ ‘: {’ ‘: {…}, ‘b’: {…}}, ‘a’: {’ ': {…}, ‘b’: {…}}}

The code creates a pair of dictionaries that are values within each other according to some key(s).

Printing the `repr` of that naively would result in infinite recursion. The implementation of `print`, `repr` or `dict.__repr__` either spots the reference cycle or terminates the infinite recursion, and replaces it with `...`.

The entire thing is the `b` dictionary, since we `print(b)`. It has one entry: `' '` is the key, and the `a` dictionary is the value. Let’s highlight those:

``````{' ': {' ': {...}}}
^^^  ^^^^^^^^^^^^
key  `a` dictionary
``````

Now, let’s examine the part that represents the `a` dictionary. That also has one entry: `' '` is the key, and the `b` dictionary is the value:

``````{' ': {' ': {...}}}
^^^  ^^^^^
key  `b` dictionary
``````

Now the `b` dictionary was written as just `{...}`. It uses `{}` so that we can see that it’s a dictionary; but we can’t write the contents normally, because then we will get in a loop (when we try to write it, we have to write `a` again, which means we have to write `b` again, etc.).

Python is smart enough to detect the problem, so it only writes `...` to say that there is more content here.