{"id":12746,"date":"2021-09-30T14:00:55","date_gmt":"2021-09-30T08:30:55","guid":{"rendered":"https:\/\/python-programs.com\/?p=12746"},"modified":"2021-11-22T18:39:29","modified_gmt":"2021-11-22T13:09:29","slug":"intersection-of-two-dictionaries-via-keys-in-python","status":"publish","type":"post","link":"https:\/\/python-programs.com\/intersection-of-two-dictionaries-via-keys-in-python\/","title":{"rendered":"Intersection of Two Dictionaries via Keys in Python"},"content":{"rendered":"

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<\/a> available.<\/p>\n

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.<\/p>\n

You’ll come across a concept called Python dictionaries here. Dictionaries are a relatively prevalent data structure in the Python programming language.<\/p>\n

Before going further into it, let us first talk about dictionaries.<\/p>\n

Dictionaries:<\/strong><\/p>\n

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.<\/p>\n

A dictionary is created in Python by enclosing numerous key-value pairs in curly braces.<\/p>\n

Examples:<\/strong><\/p>\n

Example1:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

dictionary1 = {'Hello': 'one', 'this': 'two', 'is': 'three', 'btechgeeks': 'four'} \r\ndictionary2 = {'good': 'five', 'morning': 'six', 'btechgeeks': 'four', 'is': 'three'}<\/pre>\n

Output:<\/strong><\/p>\n

The given first dictionary =  {'Hello': 'one', 'this': 'two', 'is': 'three', 'btechgeeks': 'four'}\r\nThe given second dictionary =  {'good': 'five', 'morning': 'six', 'btechgeeks': 'four', 'is': 'three'}\r\nprinting the intersection of the given two dictionaries =  {'is': 'three', 'btechgeeks': 'four'}<\/pre>\n

Example2:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

dictionary1  = {'Hello': 'one', 'this': 'two', 'igs': 'three', 'btechgeeks': 'four'} \r\ndictionary2= {'good': 'five', 'hello': 'six', 'igs': 'three'}<\/pre>\n

Output:<\/strong><\/p>\n

The given first dictionary =  {'Hello': 'one', 'this': 'two', 'igs': 'three', 'btechgeeks': 'four'}\r\nThe given second dictionary =  {'good': 'five', 'hello': 'six', 'igs': 'three'}\r\nprinting the intersection of the given two dictionaries =  {'igs': 'three'}<\/pre>\n

Intersection of Two Dictionaries via Keys in Python<\/h2>\n

Below are the ways to find the intersection of two dictionaries via keys in Python.<\/p>\n