How To Use None As A Dictionary Key In Python

Use None as a dictionary key in Python

None is an exciting object in Python that other languages ​​don’t have, so that you can use None as a dictionary key. For the answer about how to use None as a dictionary key in Python, read the following article.

Use None as a dictionary key in Python

In Python, ‘None’ is a specific object indicating that the object is missing the presence of a value. The Python Null object is the singleton None. In other words, ‘None’ in Python is similar to the ‘Null’ keyword in other programming languages.

None can be a dictionary key

All hashable objects can become keywords like None, str, int, bool, tuple, and frozenset. ‘None’ is in hashable objects, so there’s no reason why ‘None’ can’t become a dictionary key.

Example:

# Create a dictionary where a dictionary key is None.
dictObj = {'name': 'John', 'Age': 18, None: 'Student'}

# Use the get() method to get the value of the key 'None'.
print('Value of key None:', dictObj.get(None))

 

Output:

Value of key None: Student

You can test it as usual because there is no conflict. You shouldn’t have any issues with it. Although you can’t store many items in the None key because of the typical 1-to-1 Key-Value relationship, choosing None as the key shouldn’t by itself result in issues.

Convert dictionary to Json object

Example:

  • Create a dictionary with the key ‘None’.
  • Use the json.dumps() function to convert the dictionary to a Json string.
import json
 
# Create a dictionary where a dictionary key is None.
dictObj = {'name': 'John', 'Age': 18, None: 'Student'}
print('Original dictionary:', dictObj)
 
# Convert dict to Json string.
dictObj = json.dumps(dictObj)
print('Dictionary to Json String:', dictObj) 

Output:

Original dictionary: {'name': 'John', 'Age': 18, None: 'Student'}
Dictionary to Json String: {"name": "John", "Age": 18, "null": "Student"}

As you can see, the key ‘None’ in Python, converted to Json, will become ‘null’.

I will do this Json string to Python conversion to see if ‘null’ returns ‘None’.

Example:

import json
 
# Create a dictionary where a dictionary key is None.
dictObj = {'name': 'John', 'Age': 18, None: 'Student'}
print('Original dictionary:', dictObj)
 
# Convert dict to Json string.
dictObj = json.dumps(dictObj)
print('Dictionary to Json String:', dictObj)
 
# Convert Json string back to Python dictionary using json.loads() function.
dictAgain = json.loads(dictObj)
print('Json string converted back to dictionary:', dictAgain)

Output:

Original dictionary: {'name': 'John', 'Age': 18, None: 'Student'}
Dictionary to Json String: {"name": "John", "Age": 18, "null": "Student"}
Json string converted back to dictionary: {'name': 'John', 'Age': 18, 'null': 'Student'}

Note: The problem will be nothing if you set both the key and the value to ‘None’.

Example:

dictObj = {'name': 'John', 'Age': 18, None: None}
print(dictObj['None'])

Output:

Traceback (most recent call last):
 File "./prog.py", line 3, in <module>
KeyError: 'None

There is an error in the above code, so it is unnecessary to set the dictionary key as ‘None,’ which is misleading.

Use another name as the key

It’s not wrong to set ‘None’ as the key, but you should limit its use, replacing it with a name related to the value.

Example:

# Create a dictionary where a dictionary key is None.
dictObj = {'name': 'John', 'Age': 18, 'Vacations': 'Student'}
print('Value of key Vacations:', dictObj['Vacations'])

Output:

Value of key Vacations: Student

In this example, I have replaced the key ‘None’ in the above examples with the key ‘Vacations’ to optimize.

Summary

Those are ways to use None as a dictionary key in Python, but I don’t recommend using it as it would be misleading. There’s no shortage of names. You can give dictionary keys. If you have any comments, please leave a comment below. Thank you for reading the article.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *