Open In App

Python Program to find XOR of all the key value pairs in a Dictionary

Given a dictionary in python, write a program to find the XOR of all the key-value pairs in the dictionary and return in the form of an array.

Note: All the keys and values in the dictionary are integers.



Examples:

Input : dic={1:3, 4:5, 6:7, 3 :8}
Output : [2, 1, 1, 11]
Explanation: XOR of all the key-value pairs in the dictionary are [1^3, 4^5, 6^7, 3^8] 
Thus, [2, 1, 1, 11] 

Note: Order may change as the dictionary is unordered.

Method 1:



Below is the implementation of the above approach




def xorOfDictionary(dic):
    # Array to store XOR values
    arr = []
 
    # Traversing the dictionary
    for i in dic:
        # Finding XOR of Key value.
        cur = i ^ dic[i]
        arr.append(cur)
    return arr
 
 
dic = {5: 8, 10: 9, 11: 12, 1: 14}
print(xorOfDictionary(dic))

Output
[13, 3, 7, 15]

Time complexity: O(n)
Auxiliary space : O(n) for storing XOR values.

Method  2:Using items() function




def xorOfDictionary(dic):
    # Array to store XOR values
    arr = []
 
    # Traversing the dictionary
    for key, value in dic.items():
        # Finding XOR of Key value.
        cur = key ^ value
        arr.append(cur)
    return arr
 
 
dic = {5: 8, 10: 9, 11: 12, 1: 14}
print(xorOfDictionary(dic))

Output
[13, 3, 7, 15]

Time complexity: where n is the number of items in the dictionary, because it has to traverse the dictionary once to calculate the XOR of each key-value pair.
Auxiliary space: O(n), because it needs to store all the XOR values in an array.

Method #3 : Using keys() and values() methods




dic = {5: 8, 10: 9, 11: 12, 1: 14}
arr = []
x=list(dic.keys())
y=list(dic.values())
for i in range(0,len(x)):
    arr.append(x[i]^y[i])
print(arr)

Output
[13, 3, 7, 15]

Time complexity: O(n)
Auxiliary space: O(n) for storing XOR values.

Method 4: Using a list comprehension




def xorOfDictionary(dic):
  # List comprehension to compute XOR values
  return [i ^ dic[i] for i in dic]
 
dic = {5: 8, 10: 9, 11: 12, 1: 14}
print(xorOfDictionary(dic))

Output
[13, 3, 7, 15]

Time complexity: O(n)
Auxiliary space: O(n) 

Method 5:Using the map() function and a lambda function

Code uses the map() function with a lambda function to perform the XOR operation on each key-value pair in the dictionary and store the result in a list.




dict = {5: 8, 10: 9, 11: 12, 1: 14}
 
xor_res = list(map(lambda kv: kv[0] ^ kv[1], dict.items()))
 
# Result
print(xor_res)

Output
[13, 3, 7, 15]

The items() method is used by the map() function to extract the key-value pairs for each item in the dictionary. The list() method is used to turn iterable that the map() function returns into a list. The list of XOR results is printed last.

Time complexity: O(N) as the map() function and the lambda function are both applied to each key-value pair once, which takes O(1) time and there are a total of N key-value pairs. So time complexity is O(N).
Auxiliary space: O(n) as the list is of the same size as the number of key-value pairs in the dictionary. Therefore, the space complexity is O(N).


Article Tags :