In this post, we will take a look at how to declare and create a Python 4D dictionary and use has_key() and the “in” operator to check if a key is present or not. Follow along see the code examples and try them out yourself.
A Python dictionary is simply a name-value pair also called a tuple pair.
Example:
d = {"Tom": 44, "Sam": 35, "Kate": 39}
print(d)
{'Tom': 44, 'Sam': 35, 'Kate': 39}
Python Has Key (has_key) Method To Search A Dictionary
In Python 3 there is a method named has_key which you can use to search a dictionary and see if a key exists. But in the recent Python versions, this method is deprecated and you should use the “in” operator instead.
Following is an example of using the “has_key” and “in” operators to search for a key in a dictionary.
# using the "in" operator
fruits_dictionary = dict(apple= 1, mango= 3, banana= 4)
search_key = 'orange'
if search_key in fruits_dictionary:
print('Key exists')
else:
print('Key does no exist')
# using the "has_key" method
search_key = 'orange'
if fruits_dict.has_key(search_key):
print('Key found')
else:
print('Key not found')
Python multi-dimensional dictionary
In Python, the dictionary variable is simply represented by curly braces like this one { }. The following is how you declare a Python multi-dimensional dictionary.
# empty dictionary
myDictionary = { }
myDictionary[1] = { }
myDictionary[4] = { }
myDictionary[1][4] = 10
myDictionary[1][4]
# thi above lien will print the output 10
>>> 10
Python Selenium Dropdown Get Value
Take a look at more suggestions in this StackOverFlow post for more examples of Python 4D dictionary creation and usage.
from collections import defaultdict
my_dictionary = defaultdict(dict)
my_dictionary[1][2] = 5
Run this demo and see what you get
>>> new_dic_plain = {}
>>> new_dic_plain[1] = {}
>>> new_dic_plain[1][2] = 5
>>> new_dic_plain
{1: {2: 5}}
>>> new_dic_setdefault = {}
>>> new_dic_setdefault.setdefault(1, {})[2] = 5
>>> new_dic_setdefault
{1: {2: 5}}
>>> from collections import defaultdict
>>> new_dic_defaultdict = defaultdict(dict)
>>> new_dic_defaultdict[1][2] = 5
>>> new_dic_defaultdict
defaultdict(<type 'dict'>, {1: {2: 5}})
I love this way of creating multidimensional 4D or any dimension dictionary in Python. You should be able to use the has_key or the “in” operator in Pyonth to search this dictionary too.
Another example of multi-dimensional dictionary creation in python
>>> s1={“name”:”Tom”,”age”:25, “id”:44} >>> s2={“name”:”Sam”,”age”:43, “id”:3}
Python two-dimensional dictionary
A 2-dimensional dictionary can be defined in Python code like this following code:
>>>twodimdict={1:d1,2:d2}
>>>twodimdict
{1: {'name': 'Tom', 'age': 25, 'id': 44}, 2: {'name': ''Sam, 'age': 43, 'id': 3}}
Check out this example- create as many dimensions for a Python dictionary
def nested_dict(n, type):
if n == 1:
return defaultdict(type)
else:
return defaultdict(lambda: nested_dict(n-1, type))
Now this will be assigned to new_dict.
new_dict = nested_dict(2, float)
Now you can assign this way.
new_dict['key1']['key2'] += 5
Add as many dimensions as you like with your choice of target type:
new_dict = nested_dict(3, list)
new_dict['a']['b']['c'].append(5)
#Program output:
new_dict['a']['b']['c'] = [5]
Hope you found this post useful. You can try many other examples which I found here.
Flattening the dictionary
The below program flattens a dictionary and you can use it for your own dictionary no matter what the dimension id.
# Generate list of dictionaries
list_d = [
{"Full Throttle": 1995},
{"Sid Meier's Civilization II": 1996},
{"Diablo": 1996},
]
# Flatten list of dictionary
list_d_flat = {
title: year for dictionary in list_d for title, year in dictionary.items()
}
# Print flattened dictionary
print(list_d_flat)
Nested dictionary in Python
You know the nested python dictionary as shown in the example below. This is a good example where you have two dictionaries nested and assigned to the “people” variable.
people = {1: {'name': 'John', 'age': '27', 'sex': 'Male'},
2: {'name': 'Marie', 'age': '22', 'sex': 'Female'}}
print(people)
#output
{1: {'name': 'John', 'age': '27', 'sex': 'Male'}, 2: {'name': 'Marie', 'age': '22', 'sex': 'Female'}}
Conclusion
You should use the latest Python version which supports the new “in” function which is better than the has_key” function. Hope the examples provided in this post helped you understand all about the has_key method in Python.
Remember, you can create a python dictionary of any dimension even using a “defaultdict” from the collection library. Import it from collections and use it directly as shown below.
from collections import defaultdict
new_dic = defaultdict(dict)
new_dic[1][2]=5
>>>new_dic
defaultdict(<type 'dict'>, {1: {2: 5}})
You might be interested in this post: How To Delete FB Group On iPhone And Computer
Leave a Reply