Python dictionary intersection: If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list available.
In Python, we will address the problem of intersecting two dictionaries via their keys. As a result, something in common between the two dictionaries is required.
You’ll come across a concept called Python dictionaries here. Dictionaries are a relatively prevalent data structure in the Python programming language.
Before going further into it, let us first talk about dictionaries.
Dictionaries:
Python dictionaries are mutable collections of things that contain key-value pairs. The dictionary contains two main components: keys and values. These keys must be single elements, and the values can be of any data type, such as list, string, integer, tuple, and so on. The keys are linked to their corresponding values. In other words, the values can be retrieved using their corresponding keys.
A dictionary is created in Python by enclosing numerous key-value pairs in curly braces.
- Python Program to Form a Dictionary from an Object of a Class
- Python Program to Map Two Lists into a Dictionary using Zip(), Append() Functions | How to Combine Two Lists into a Dictionary?
- Java: How to get all Keys by a Value in HashMap ? | Search by Value in Map
Examples:
Example1:
Input:
dictionary1 = {'Hello': 'one', 'this': 'two', 'is': 'three', 'btechgeeks': 'four'} dictionary2 = {'good': 'five', 'morning': 'six', 'btechgeeks': 'four', 'is': 'three'}
Output:
The given first dictionary = {'Hello': 'one', 'this': 'two', 'is': 'three', 'btechgeeks': 'four'} The given second dictionary = {'good': 'five', 'morning': 'six', 'btechgeeks': 'four', 'is': 'three'} printing the intersection of the given two dictionaries = {'is': 'three', 'btechgeeks': 'four'}
Example2:
Input:
dictionary1 = {'Hello': 'one', 'this': 'two', 'igs': 'three', 'btechgeeks': 'four'} dictionary2= {'good': 'five', 'hello': 'six', 'igs': 'three'}
Output:
The given first dictionary = {'Hello': 'one', 'this': 'two', 'igs': 'three', 'btechgeeks': 'four'} The given second dictionary = {'good': 'five', 'hello': 'six', 'igs': 'three'} printing the intersection of the given two dictionaries = {'igs': 'three'}
Intersection of Two Dictionaries via Keys in Python
Below are the ways to find the intersection of two dictionaries via keys in Python.
Method #1: Using Dictionary Comprehension
Approach:
- Give the two dictionaries as static input and store them in two variables.
- Next, take dictionary1’s key as x and perform a for loop to see if the x in dictionary1 also exists in dictionary2. If it does, the common key and its value are moved to a new dictionary named intersectdicts.
- Print the new dictionary intersectdicts with the common keys and their values.
- The Exit of the Program.
Below is the implementation:
# Give the two dictionaries as static input and store them in two variables. dictionary1 = {'Hello': 'one', 'this': 'two', 'is': 'three', 'btechgeeks': 'four'} dictionary2 = {'good': 'five', 'morning': 'six', 'btechgeeks': 'four', 'is': 'three'} # printing the original given two dictionaries print("The given first dictionary = ", dictionary1) print("The given second dictionary = ", dictionary2) # Next, take dictionary1's key as k and perform a for loop # to see if the k in dictionary1 also exists in dictionary2. If it does, the common key # and its value are moved to a new dictionary named intersectdicts. # intersection intersectdicts = {k: dictionary1[k] for k in dictionary1 if k in dictionary2} # Print the new dictionary intersectdicts with the common keys and their values. print("printing the intersection of the given two dictionaries = ", (intersectdicts))
Output:
The given first dictionary = {'Hello': 'one', 'this': 'two', 'is': 'three', 'btechgeeks': 'four'} The given second dictionary = {'good': 'five', 'morning': 'six', 'btechgeeks': 'four', 'is': 'three'} printing the intersection of the given two dictionaries = {'is': 'three', 'btechgeeks': 'four'}
Method #2: Using & Operator
Approach:
- Give the two dictionaries as static input and store them in two variables.
- Then, using the items() function, convert the dictionaries dictionary1 and dictionary2 into list format. Then, using the & operator, perform their AND operation. The common key-value pairs are then converted into a dictionary and stored in intersectdicts using dict().
- Print the new dictionary intersectdicts with the common keys and their values.
- The Exit of the Program.
Below is the implementation:
# Give the two dictionaries as static input and store them in two variables. dictionary1 = {'Hello': 'one', 'this': 'two', 'is': 'three', 'btechgeeks': 'four'} dictionary2 = {'good': 'five', 'morning': 'six', 'btechgeeks': 'four', 'is': 'three'} # printing the original given two dictionaries print("The given first dictionary = ", dictionary1) print("The given second dictionary = ", dictionary2) # Then, using the items() function, # convert the dictionaries dictionary1 and dictionary2 into list format. # Then, using the & operator, perform their AND operation. # The common key-value pairs are then # converted into a dictionary and stored in intersectdicts using dict(). intersectdicts = dict(dictionary1.items() & dictionary2.items()) # Print the new dictionary intersectdicts with the common keys and their values. print("printing the intersection of the given two dictionaries = ", (intersectdicts))
Output:
The given first dictionary = {'Hello': 'one', 'this': 'two', 'is': 'three', 'btechgeeks': 'four'} The given second dictionary = {'good': 'five', 'morning': 'six', 'btechgeeks': 'four', 'is': 'three'} printing the intersection of the given two dictionaries = {'is': 'three', 'btechgeeks': 'four'}
Related Programs:
- python how to create a list of all the keys in the dictionary
- program for addition of two matrices in python cpp
- python program to find the minimum difference between the index of two given elements present in an array
- dictionaries in python
- how to create and initialize a list of lists in python
- python how to find all indexes of an item in a list
- basics of python built in types