Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get union keys of `a` and `b` dictionary and 'a' values? [duplicate]

I was doing django project about processing request.data and from.cleaned_data issues. When user only inputs specify fields, it will send request to my server. Next, the form class process the request, except processing inputed fields and return no inputted fields from form built-in fields.

This is request data:

<QueryDict: {u'is_public': [u'True']}>

This is cleaned data from from class:

{'name': u'', 'devie_type': u'', 'is_public': True, 'serial_num': u'', 'is_online': False, 'operation_system': u''}

I know these are dictionary type. I hope getting their union keys of them and values of cleaned data. I expect that it returns:

{u'is_public': True}

This is my attempt:

a = {}
for k in request.data:
    if k in the_form.cleaned_data:
        a[k] = the_form.cleaned_data[k]
print a

Is it readable? or is there any built-in functions about processing union dictionary in python?

like image 840
Burger King Avatar asked Oct 28 '15 02:10

Burger King


People also ask

How to get the keys of a dictionary in Python?

In this tutorial, we will look at how to get the keys of a Python dictionary with the help of some examples. How to get the keys of a dictionary in Python? You can use the Python dictionary keys () function to get all the keys in a Python dictionary. The following is the syntax: It returns a dict_keys object containing the keys of the dictionary.

What is the difference between keys and values in a dictionary?

The keys here are the employee names whereas the values are their respective departments. Let’s get all the keys in the dictionary using the dictionary’s keys () function. You can see that we get all the names of the employees (the keys in the dictionary employee) in a dict_keys object.

What is the return value of dictionary dictionary?

Returns: A view object is returned that displays all the keys. This view object changes according to the changes in the dictionary. Note: The order of these key values in the list may not always be the same. Here, when the dictionary is updated, keys are also automatically updated to show the changes.

How to merge two dictionaries based on their keys in Python?

We all have those moments when we want to merge two Python dictionaries together based on their keys. The traditional way to do this was to use the dict.update () method.


1 Answers

You can use intersection operations (which sounds like what you really want, not union) to efficiently limit iteration to keys common to both dicts, then use a dict comprehension to achieve this fairly efficiently:

a = {k: the_form.cleaned_data[k]
     for k in request.data.viewkeys() & the_form.cleaned_data.viewkeys()}

So when a key exists in both the request and the cleaned data, you'll have a mapping from that key to the cleaned data. All other keys that appear in only one input dict or the other are dropped. In Python 3, you'd replace .viewkeys() with just .keys() (and this won't work before Python 2.7, which is where .viewkeys() was introduced).

like image 89
ShadowRanger Avatar answered Oct 06 '22 20:10

ShadowRanger