Python Forum
[closed] Empty dictionary with None
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[closed] Empty dictionary with None
#1
Hi,
I'm spending hours in understanding why a 'None' appears immediatly after creating a dictionary, leading to a crash afterward (such dictionary is created in a function and returned)

Here is a basic example without any 'None'
import numpy as np

Mat = np.random.random( (2, 2) )
MyDict = {}
print(MyDict)
MyDict.update({'Mat': Mat},)
DictList = list(MyDict.keys())

def test(A):
    DefDict = {}
    print(DefDict)
    DefDict.update({'A': A})
    return DefDict

B = test(Mat)
Output:
{} {}
In my real case:
RealDict = {}
print(RealDict)
Output:
{} None
That's the first time the dictionary is created: any idea to explain from where 'None' comes from?

Thanks

Paul
Reply
#2
MyDict = {} Creates an empty dictionary, thus None is it's value until you actually add items.

>>> MyDict = {}
>>> print(MyDict)
{}
>>> key = 2
>>> kvalue = 2
>>> MyDict[key] = kvalue
>>> print(MyDict)
{2: 2}
You can then add additional data
>>> another_key = 'Food'
>>> another_value = 'Banana'
>>> MyDict[another_key] = another_value
>>> print(MyDict)
{2: 2, 'Food': 'Banana'}
>>>
>>> print(MyDict['Food'])
Banana
>>>
Reply
#3
Thanks Larz60+

Nonetheless the trouble I'm facing comes from the "none" into the dictionary (my code crashes because of it and because it's not supossed to be there: either the dictionary is empty, either it's composed of array).

Of course, I guessed it comes from my initialization, but I'm still wondering why?

Curently my workflow has been:
  1. the dictionary is initialized (remains empty accordingly)
  2. when specific conditions are reached, then it is updated (both keys and values are "variables")
  3. the content of the dictionary is listed and additional calculations on arrays are performed ((but the None appears here)

Another way would be to create the dictionary when the first the condition is met, but it request to be able to test is the dictionary exists, and I still don't know how (may be a bit naive)?

I guess I'm still missing something, but I'm looking into a way to fix it

Paul
Reply
#4
(Apr-01-2022, 10:04 PM)paul18fr Wrote: In my real case:
RealDict = {}
print(RealDict)
Output:
{} None
I am sorry Paul but I do not understand you. What is the relation between this "real case" and the script you showed us?
When I run this "real case" obviously only {} is printed.
Reply
#5
sorry to have been not clear

The case I provided first is what I expect every time I use a dictionary, and I've used exactly the same steps so far; but in my real case (and I do not understand why), when I create an empty dictionary, both "{}" and "None" are inside leading to a crash further in the code.
Reply
#6
Please show the real code that demonstrates the problem.
buran likes this post
Reply
#7
You cannot change the definition of the dictionary object (without consequences)
None is the known value of an empty dictionary, always.
If you need to test for that, you can check that there are values:
>>> MyDict = {}
>>> if MyDict:
...     print(f"MyDict has assigned values: {MyDict}")
... else:
...     print(f"MyDict is empty: {MyDict}")
... 
MyDict is empty: {}
>>> MyDict['apples'] = 'Fruit'
>>> if MyDict:
...     print(f"MyDict has assigned values: {MyDict}")
... else:
...     print(f"MyDict is empty: {MyDict}")
... 
MyDict has assigned values: {'apples': 'Fruit'}
Reply
#8
Thanks all for contributions and explanations.

It's mean my trouble has a different origin, and I need to find it
Reply
#9
If you're having trouble working out where the None is coming from, print out your variables at different points and check whether they are what you think they should be and whether execution is entering the branches you think. Either that, or use a debugger to step through the program as you run it.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020