dictionary into desired variable....

Chris Angelico rosuav at gmail.com
Fri Aug 10 10:31:07 EDT 2012


On Sat, Aug 11, 2012 at 12:02 AM, Tamer Higazi <th982a at googlemail.com> wrote:
> Hi!
> suppose you have a dictionary that looks like this:
>
> x = [1,3,6,1,1] which should represent a certain other variable.
>
> in reality it would represent:
>
> y[1][3][6][1][1]
>
> Now, how do I write a python routine, that points in this dictionary,
> where I should receive or set other values.

For a start, that looks like a list, not a dictionary. A dict in
Python is a mapping, eg a hashtable - an unordered pairing of keys and
values. But this might do what you want:

value = y
for idx in x:
   value = value[idx]

At the end of that, 'value' will be the same as 'y[1][3][6][1][1]'.

If that's not what you mean, you may want to clarify your question some.

Hope that helps!

Chris Angelico



More information about the Python-list mailing list