Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python – Cross mapping of Two dictionary value lists
When it is required to cross-map two dictionary valued lists, the setdefault and extend methods are used. This technique allows you to map values from one dictionary to another based on matching keys and indices.
Understanding Cross Mapping
Cross mapping involves using values from the first dictionary as keys to lookup corresponding values in the second dictionary. The result combines these lookups into a new dictionary structure.
Example
Below is a demonstration of cross mapping two dictionaries −
my_dict_1 = {"Python" : [4, 7], "Fun" : [8, 6]}
my_dict_2 = {6 : [5, 7], 8 : [3, 6], 7 : [9, 8]}
print("The first dictionary is : " )
print(my_dict_1)
print("The second dictionary is : " )
print(my_dict_2)
my_result = {}
for key, value in my_dict_1.items():
for index in value:
my_result.setdefault(key, []).extend(my_dict_2.get(index, []))
print("The resultant dictionary is : ")
print(my_result)
The first dictionary is :
{'Python': [4, 7], 'Fun': [8, 6]}
The second dictionary is :
{6: [5, 7], 8: [3, 6], 7: [9, 8]}
The resultant dictionary is :
{'Python': [9, 8], 'Fun': [3, 6, 5, 7]}
How It Works
The cross mapping process works as follows:
- Step 1: Iterate through each key-value pair in the first dictionary
- Step 2: For each value list, use individual elements as lookup keys in the second dictionary
-
Step 3: Use
setdefault()to initialize empty lists for new keys in the result -
Step 4: Use
extend()to add found values from the second dictionary
Breaking Down the Example
Let's trace through the mapping process −
# For "Python": [4, 7]
# - Look up key 4 in dict_2: not found, skip
# - Look up key 7 in dict_2: found [9, 8]
# Result: "Python": [9, 8]
# For "Fun": [8, 6]
# - Look up key 8 in dict_2: found [3, 6]
# - Look up key 6 in dict_2: found [5, 7]
# Result: "Fun": [3, 6, 5, 7]
my_dict_1 = {"Python" : [4, 7], "Fun" : [8, 6]}
my_dict_2 = {6 : [5, 7], 8 : [3, 6], 7 : [9, 8]}
print("Step by step mapping:")
for key, values in my_dict_1.items():
print(f"\nProcessing '{key}': {values}")
for val in values:
if val in my_dict_2:
print(f" Found {val} -> {my_dict_2[val]}")
else:
print(f" {val} not found in second dictionary")
Step by step mapping: Processing 'Python': [4, 7] 4 not found in second dictionary Found 7 -> [9, 8] Processing 'Fun': [8, 6] Found 8 -> [3, 6] Found 6 -> [5, 7]
Key Methods Used
- setdefault(): Creates a new key with empty list if it doesn't exist, otherwise returns existing value
- extend(): Adds all elements from one list to another list
- get(): Safely retrieves values from dictionary, returns empty list if key not found
Conclusion
Cross mapping dictionaries using setdefault() and extend() provides an efficient way to combine data from multiple dictionaries. This technique is useful for data transformation and merging operations where keys in one dictionary correspond to values in another.
