For specific keys , extract non empty values in a dictionary
*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
Re: For specific keys , extract non empty values in a dictionary
Ganesh Pal wrote: > *How do I check few specific/selected keys in a dictionary and extract > their values if they are not empty* You mean not None. > 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"] > *Output:* > > *1 3 9* >>> wanted = {"one", "three", "seven", "nine"} >>> {k: o_num[k] for k in wanted & o_num.keys() if o_num[k] is not None} {'one': 1, 'nine': 9, 'three': 3} > I am a Python 2.7 user and on Linux box You have to replace keys() with viewkeys() in Python 2. -- https://mail.python.org/mailman/listinfo/python-list
Re: For specific keys , extract non empty values in a dictionary
> >>> {k: o_num[k] for k in wanted & o_num.keys() if o_num[k] is not None} Thanks peter this looks better , except that I will need to use the logial 'and' operator or else I will get a TypeError >>> {k: o_num[k] for k in wanted & o_num.keys() if o_num[k] is not None} TypeError: unsupported operand type(s) for &: 'set' and 'list' Example : >>>print {k: o_num[k] for k in wanted and o_num.keys() if o_num[k] is not None} {'nine': 9, 'five': 5, 'three': 3, 'one': 1} On Sat, Jun 16, 2018 at 11:12 PM, Peter Otten <__pete...@web.de> wrote: > Ganesh Pal wrote: > > > *How do I check few specific/selected keys in a dictionary and extract > > their values if they are not empty* > > You mean not None. > > > 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"] > > > *Output:* > > > > *1 3 9* > > >>> wanted = {"one", "three", "seven", "nine"} > >>> {k: o_num[k] for k in wanted & o_num.keys() if o_num[k] is not None} > {'one': 1, 'nine': 9, 'three': 3} > > > I am a Python 2.7 user and on Linux box > > You have to replace keys() with viewkeys() in Python 2. > > -- > https://mail.python.org/mailman/listinfo/python-list > -- https://mail.python.org/mailman/listinfo/python-list
Re: For specific keys , extract non empty values in a dictionary
On 2018-06-17 15:47, Ganesh Pal wrote: >>> {k: o_num[k] for k in wanted & o_num.keys() if o_num[k] is not None} Thanks peter this looks better , except that I will need to use the logial 'and' operator or else I will get a TypeError {k: o_num[k] for k in wanted & o_num.keys() if o_num[k] is not None} TypeError: unsupported operand type(s) for &: 'set' and 'list' Peter said that you need to use viewkeys() instead of keys() in Python 2: >>> {k: o_num[k] for k in wanted & o_num.viewkeys() if o_num[k] is not None} Using 'and' instead won't give the correct result. Example : print {k: o_num[k] for k in wanted and o_num.keys() if o_num[k] is not None} {'nine': 9, 'five': 5, 'three': 3, 'one': 1} On Sat, Jun 16, 2018 at 11:12 PM, Peter Otten <__pete...@web.de> wrote: Ganesh Pal wrote: > *How do I check few specific/selected keys in a dictionary and extract > their values if they are not empty* You mean not None. > 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"] > *Output:* > > *1 3 9* >>> wanted = {"one", "three", "seven", "nine"} >>> {k: o_num[k] for k in wanted & o_num.keys() if o_num[k] is not None} {'one': 1, 'nine': 9, 'three': 3} > I am a Python 2.7 user and on Linux box You have to replace keys() with viewkeys() in Python 2. -- https://mail.python.org/mailman/listinfo/python-list
Re: For specific keys , extract non empty values in a dictionary
Dennis Lee Bieber wrote: > On Sun, 17 Jun 2018 17:37:25 +0100, MRAB > declaimed the following: > >>On 2018-06-17 15:47, Ganesh Pal wrote: >>> {k: o_num[k] for k in wanted & o_num.keys() if o_num[k] is not >>> {None} >>> >>> Thanks peter this looks better , except that I will need to use the >>> logial 'and' operator or else I will get a TypeError >>> >> {k: o_num[k] for k in wanted & o_num.keys() if o_num[k] is not None} >>> >>> TypeError: unsupported operand type(s) for &: 'set' and 'list' >>> >>Peter said that you need to use viewkeys() instead of keys() in Python 2: >> >> >>> {k: o_num[k] for k in wanted & o_num.viewkeys() if o_num[k] is not >>None} >> > > What's wrong with the simple > 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" ] { k : o_num[k] for k in args_list if o_num.get(k, None) is not None } > {'nine': 9, 'three': 3, 'one': 1} It's hidden here because of the removal of None values, but in the generic case when you say "give me all x in a that are also in b" instead of "give me the intersection of a and b" you are overspecifying the problem. -- https://mail.python.org/mailman/listinfo/python-list