Can I iterate over a dictionary outside a function ?

Oscar Benjamin oscar.j.benjamin at gmail.com
Thu Apr 11 05:58:54 EDT 2013


On 11 April 2013 10:48, inshu chauhan <insideshoes at gmail.com> wrote:
> I have a prog in which a functions returns a dict but when I try to iterate
> over the dict using iterkeys, It shows an error. I think its because only
> address of the dictionary is returned so cannot be iterated upon.
>
> Please suggest some way by which it can be made possible to iterate over the
> dictionary using  iterkeys outside the function ?

You would probably get a more helpful answer if you showed the code
that is giving you a problem. See here:
http://sscce.org/

Your question makes no sense to me. It is perfectly possible to
iterate over a dict returned from a function:

>>> def function():
...     d = {'asd':123, 'qwe': 456}
...     return d
...
>>> a = function()
>>> a
{'qwe': 456, 'asd': 123}
>>> for x in a.iterkeys():
...     print(x)
...
qwe
asd


Oscar



More information about the Python-list mailing list