*How do I check  few specific/selected  keys in a dictionary and extract
their values if they are not empty*



*Example : Extract the values  for key "one","three","seven"  and "nine” if
they are not empty*



*Input :*

*o_num  = {'one': 1,*

*          'three': 3,*

*          'bar': None,*

*          'five' : 5,*

*          'rum' : None,*

*          'seven' : None,*

*          'brandy': None,*

*          'nine' : 9,*

*          'gin': None}*



*Output:*

*1 3 9*



Here is my solution , Please  review the below code and let me know your
suggestion .



#/usr/bin/python



o_num  = {'one': 1,

          'three': 3,

          'bar': None,

          'five' : 5,

          'rum' : None,

          'seven' : None,

          'brandy': None,

          'nine' : 9,

          'gin': None}





args_list = ["one","three","seven","nine"]



args_dict = dict( (k,v) for k, v in o_num.items() if v and k in args_list )



print args_dict



o/p:

root@X1:/Play_ground/DICT# python hello.py

{'nine': 9, 'three': 3, 'one': 1}





Also,  Is unpacking the elements in the o_num  dictionary as shown below
fine  or are there any other alternatives



arg1, arg2, arg3, arg4, = map(args_dict.get, ('one', 'three', 'seven',
'nine'))

print arg1, arg2, arg3, arg4





I am a Python 2.7 user and on Linux box



Regards,

Ganesh
-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to