Ramesh Raithatha
Ramesh Raithatha

Reputation: 544

Access dictionary from a list

I have some dictionaries as follows

d1={}  
d2={}  
d3={}  

Values of dictionary keys contains list
and a list,

l1=[d1,d2,d3]

list contains names of all the dictionaries available. I want to iterate through all dictionaries available through the list which contains all dictionary names.

How can i access all this dictionaries through list ?

Upvotes: 0

Views: 203

Answers (3)

StefanNch
StefanNch

Reputation: 2619

Do you need "gettattr"?

http://docs.python.org/2/library/functions.html#getattr

http://effbot.org/zone/python-getattr.htm

class MyClass:
    d1 = {'a':1,'b':2,'c':3}
    d2 = {'d':4,'e':5,'f':6}
    d3 = {'g':7,'h':8,'i':9}

myclass_1 = MyClass()    
list_1 = ['d1','d2','d3']

dict_of_dicts = {}
for k in list_1:
    dict_of_dicts[k] = getattr(myclass_1, k)

print dict_of_dicts

or if you want to apply this "globally" read how to use "getattr" relative to a module here: __getattr__ on a module

Upvotes: 0

Inbar Rose
Inbar Rose

Reputation: 43487

d1 = {'a': [1,2,3], 'b': [4,5,6]}
d2 = {'c': [7,8,9], 'd': [10,11,12]}
d3 = {'e': [13,14,15], 'f': [16,17,18]}

l1 = [d1,d2,d3]

for idx, d in enumerate(l1):
    print '\ndictionary %d' % idx
    for k, v in d.items():
        print 'dict key:\n%r' % k
        print 'dict value:\n%r' % v

Produces:

dictionary 0
dict key:
'a'
dict value:
[1, 2, 3]
dict key:
'b'
dict value:
[4, 5, 6]

dictionary 1
dict key:
'c'
dict value:
[7, 8, 9]
dict key:
'd'
dict value:
[10, 11, 12]

dictionary 2
dict key:
'e'
dict value:
[13, 14, 15]
dict key:
'f'
dict value:
[16, 17, 18]

Upvotes: 0

HennyH
HennyH

Reputation: 7944

>>> l1 = [d1,d2,d3]
>>> for d in l1:
        for k,v in d.items():
              print(k,v)

A better example

d1 = {"a":"A"}
d2 = {"b":"B"}
d3 = {"c":"C"}
l1 = [d1,d2,d3]
for d in l1:
    for k,v in d.items():
        print("Key = {0}, Value={1}".format(k,v))

Produces

>>> 
Key = a, Value=A
Key = b, Value=B
Key = c, Value=C

If they contain only the names of the dictionaries i.e "d1" you can do something like this (which produces the same result as above):

d1 = {"a":"A"}
d2 = {"b":"B"}
d3 = {"c":"C"}
l1 = ['d1','d2','d3']
for dname in l1:
    for k,v in globals()[dname].items():
        print("Key = {0}, Value={1}".format(k,v))

Though I wouldn't recommend such an approach. (note: you could also you locals() if the dictionaries were in the local scope)

When you have a dictionary which has a list associated with a key you can go over the list like so:

d1 = {"a":[1,2,3]}
d2 = {"b":[4,5,6]}
l1=["d1","d2"]

for d in l1:
    for k,v in globals()[d].items(): #or simply d.items() if the values in l1 are references to the dictionaries
        print("Dictionray {0}, under key {1} contains:".format(d,k))
        for e in v:
            print("\t{0}".format(e))

Producing

Dictionray d1, under key a contains:
    1
    2
    3
Dictionray d2, under key b contains:
    4
    5
    6

Upvotes: 5

Related Questions