Filter dictionary for values which are not in another dictionary

python filter list of dictionaries by value
python filter dictionary by value
filter nested dictionary python
python filter dictionary by key startswith
sort dictionary by value python
python dictionary
filter dictionary by list
python filter dictionary regex

The two dictionaries mydict1 and mydict2 are given. Key-value-pairs in mydict2 should be removed if the values are already contained in mydict1, regardless of the key and regardless of the order of the values.

The code below delivers the correct output mydict2 = {'key6': [2,1,4], 'key4': [2]}. However, it will be used as part of a larger code. Is there is a better, i.e. more phytonic, way to write it in order to make it more compact and effective without the need of functions?

mydict1 = {'key1':[1],'key2':[1,2],'key3':[1,2,3]}
mydict2 = {'key4':[2],'key5':[2,1],'key6':[2,1,4]}
mydict3 = {}

for md2 in mydict2:
    isindict = False
    for md1 in mydict1:
        isindict = isindict|(sorted(mydict1[md1])==sorted(mydict2[md2]))
    if not isindict:
        mydict3[md2] = mydict2[md2]
mydict2 = mydict3

The solutions for removing items from a list, comparison of dictionaries and conditional filtering of a dictionary are not transferable in a straightforward way.

Use a list comprehension and dict comprehension to sort all the values of mydict2 then check if the sorted value of mydict1 is in there:

mydict1 = {'key1':[1],'key2':[1,2],'key3':[1,2,3]}
mydict2 = {'key4':[2],'key5':[2,1],'key6':[2,1,4]}

vals = [tuple(sorted(x)) for x in mydict1.values()]
mydict2 = {k:v for (k,v) in mydict2.items() if tuple(sorted(v)) not in vals}


Or, in one line:

mydict1 = {'key1':[1],'key2':[1,2],'key3':[1,2,3]}
mydict2 = {'key4':[2],'key5':[2,1],'key6':[2,1,4]}

mydict2 = {k:v for (k,v) in mydict2.items() 
        if tuple(sorted(v)) not in [tuple(sorted(x)) for x in mydict1.values()]}


Python : Filter a dictionary by conditions on keys or values , dictionary and add elements with even key to an another dictionary i.e.. newDict = dict(). # Iterate over all the items in dictionary and filter items� dic = dic.Where(i => i.Value.BooleanProperty) .ToDictionary(i => i.Key, i => i.Value); If you can't create a new dictionary and need to alter the old one for some reason (like when it's externally referenced and you can't update all the references:

You can use dict comprehension :

mydict1 = {'key1':[1],'key2':[1,2],'key3':[1,2,3]}
mydict2 = {'key4':[2],'key5':[2,1],'key6':[2,1,4]}

results = {key : value for (key,value) in mydict2.items() 
           if set(value) not in [ set(val) for val in mydict1.values()]}

{'key4': [2], 'key6': [2, 1, 4]}


Doesn't work wiht duplicate like this :

mydict1 = {'key1':[1],'key2':[1,2],'key3':[1,2,3], 'key4' : [1, 1, 2]}
mydict2 = {'key4':[2],'key5':[2,1],'key6':[2,1,4], 'key7' : [2, 2, 1]}
Edit 2

Following @Ruzihm awsers and @Peter Wood comments :

mydict3 = {k:v for (k,v) in mydict1.items() 
        if tuple(sorted(v)) not in set(tuple(sorted(x)) for x in mydict2.values())}

This awser the OP question and avoid creating multiple time the same tuple of value as mentioned by @Peter Wood.

Python, In Python, sometimes we require to get only some of the dictionary keys and not all. This problem is quite common in web development that we require to get� Python Filter() Function. The filter() function accepts only two parameters. The first argument is the name of a user-defined function, and second is iterable like a list, string, set, tuple, etc.

i think this will work:

mydict1 = {'key1':[1],'key2':[1,2],'key3':[1,2,3]}
mydict2 = {'key3':[2],'key5':[2,1],'key6':[2,1,4]}

def merge_two_dicts(x, y):
    z = x.copy()
    return z

result = merge_two_dicts(mydict1, mydict2)

Python, printing initial dictionary. print ( "initial lists" , str (ini_dict)). # filter dictionary such that no value is greater than 0. result = dict ((k, v) for k, v in ini_dict.items() if v >� Dictionary is quite a useful data structure in programming that is usually used to hash a particular key with value, so that they can be retrieved efficiently. Let’s discuss various ways of swapping the keys and values in Python Dictionary.

15 things you should know about Dictionaries in Python, Dictionaries are written with curly brackets ({}), including key-value pairs the . update() method accepts as an argument not only another dictionary, but We can filter the products with a price lower than 100 euros using both� This code example shows how to enumerate the values in the dictionary using the Values property, and how to enumerate the keys and values in the dictionary. This code example is part of a larger example provided for the Dictionary<TKey,TValue> class ( openWith is the name of the Dictionary used in this example).

dict manual page - Tcl Built-In Commands, Non-existent keys are treated as if they map to an empty string. dict filter dictionaryValue value globPattern: The value rule only matches those key/value Data for another employee dict set employeeInfo 98372-J forenames "Anne" dict set� Definition and Usage. The values() method returns a view object. The view object contains the values of the dictionary, as a list. The view object will reflect any changes done to the dictionary, see example below.

How to Filter a List of Dictionaries in Python?, Each dictionary consists of one or more (key, value) pairs. You want to filter them by value of a particular dictionary key whether a dictionary passes the filter or not, i.e.,� The review scores are stored in the dictionary values, which means you will have to loop over the dictionary. Unfortunately (not really though), you can not simply use a for-loop to go over the dictionary object. Python dictionary items not only have both a key and a value, but they also have a special iterator to loop over them.

  • You are creating the same list of tuples for every key/value pair in mydict1
  • @PeterWood If you want to memoize the list of tuples, then use the version with the extra line vals=...
  • @PeterWood I guess I'm not sure what you're getting at because my answer only creates a single list of tuples in the first code example, not a list for each key/value pair.
  • What if the order of elements in value are different?
  • Fails with [1, 1, 2], [2, 2, 1]