1

i have dict and list as bellow

a = {'aaa': 1, 'bbb': 2, 'ccc': 3}
b = ['aaa', 'bbb', 'ddd', 'eee']

I am comparing list b in dict a and I am printing values of 'aaa' and 'bbb', I dont know how to store values of aaa=1, bbb=2. I am trying using this code

for i in range(0,len(b)):
    if b[i] not in d:
        continue
    else:
        print int(a[b[i]])

its printing

1
2

i want to assign aaa=1,bbb=2,ddd=0,eee=0.

And Then when I print "print aaa,bbb,ccc,ddd" it should print values. The variables in list b can vary, so that I should dynamically assign values which are in b not in a to zero.

Rik Poggi
  • 28,332
  • 6
  • 65
  • 82
user1182090
  • 301
  • 4
  • 7
  • 20

3 Answers3

2

Are you trying to do something like this:

for key in b:
    print key, a.get(key,0)

If you really need those new keys to be into your dictionary, you can do it like this:

for key in b:
    if a.get(key) is None:
        a[key] = 0

and print after like you normally would:

for k,v in a.items():
    print k,v

Edit: From your comment it seems that you're now doing:

eval('aaa+bbb+ccc',b)
for key in b:
    if a.get(key) is None:
        a[key] = 0
eval('aaa+bbb+ccc',b)

Are you using eval for printing? If you are, don't do that! You can find at Why should exec() and eval() be avoided? a more complete discussion about eval.

Anyway I'm guessing you might be looking for somthing like:

print sum(a[key] for kay in b)

But I really don't know. Please edit your question explaining what you're really trying to do (with samples of input and expected output).

Community
  • 1
  • 1
Rik Poggi
  • 28,332
  • 6
  • 65
  • 82
  • Thanks @riki poggi, but i want to use these values in eval('aaa+bbb+ccc',b)for key in b: if a.get(key) is None: a[key] = 0 eval('aaa+bbb+ccc',b) – user1182090 Mar 02 '12 at 11:35
  • 1
    @user1182090: What are you exavtly trying to do? Please [edit](http://stackoverflow.com/posts/9532178/edit) your question and provide more information. Also you should not use `eval` as explained in the link I provided above. – Rik Poggi Mar 02 '12 at 11:53
  • sorry @Rik .suppose dict1={'a':1,'b':2,'c':3} and list1=['a','b','c','d']. what i want in output is variables should be dynamically generated and should contain the value of 'a','b' of dict1 because 'a' and 'b' are present in list1 and for 'c' and 'd' it should contain "zero" because 'c' and 'd' of list1 are not in dict so it contains zero. I dont want this output values in different list r in dict, it should be stored in variables. so i can use it anywhere... – user1182090 Mar 03 '12 at 05:30
1

Try this

a = {'aaa': 1, 'bbb': 2, 'ccc': 3}
b = ['aaa', 'bbb', 'ddd', 'eee']
c = dict([(x, a.get(x, 0)) for x in a])

print "%(aaa)s %(bbb)s %(ccc)s" % c
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Nilesh
  • 20,521
  • 16
  • 92
  • 148
  • Thanks@lafada.. The output value is what i want, but can u say how to store the values into a dynamic variables instead of one more dict.. – user1182090 Mar 03 '12 at 05:35
0

I suggest to change the code to

for v in b:
    if v not in a:
        a[v]=0
    print a[v], '=', int(a[v])

You could use a defaultdict rather than a dict.

hochl
  • 12,524
  • 10
  • 53
  • 87
  • Thanks@hochl but can u say how to generate variables dynamically and then i hav to asssign that values for that. For eg by comparing list"b" in dict "a" if key is their it should take that value of key and dynamically generate variable and assign to it. finally elements which r not their in list"b" should be assigned zero and saved – user1182090 Mar 02 '12 at 12:00
  • @user1182090: What you're explaining is a really bad design, why do you need that in the first place? – Rik Poggi Mar 02 '12 at 12:10
  • You might be interested in reading here: http://stackoverflow.com/a/8028785/589206 – hochl Mar 02 '12 at 12:13