i wrote a little function to sort a little dict like
unsorted_dict = {
"A": [2,4,6,1,2,8,0],
"Z": 20,
"B": {
4: "hallo",
0: [4,4,4,5,1],
1: {
"b": "Potato",
"a": [ "b", "d", "c"]
}
}
}
And in theorie it works.
After the funktion below, my LIST looks like this
[{'A': None, 'B': None, 'Z': None}, [0, 1, 2, 2, 4, 6, 8], {0: None, 1: None, 4: None}, [1, 4, 4, 4, 5], {'a': None, 'b': None}, ['b', 'c', 'd'], 'Potato', 'hallo', 20]
LIST = list()
def sort(unsorted):
""" function to create a list from a dict. """
if type(unsorted) is dict:
unsorted = dict(sorted(unsorted.items()))
LIST.append(unsorted)
for key, value in unsorted.items():
unsorted[key]= sort(value)
elif type(unsorted) is int:
LIST.append(unsorted)
elif type(unsorted) is str:
LIST.append(unsorted)
else:
""" bookmark """
unsorted = sorted(unsorted)
LIST.append(unsorted)
my question is, why the values of the dict disappear during the regressive list-appending.
its a qiet nice side-effect because i can easy fill the "empty" dicts with the elements placed on the right of them, but why does it work?
After the first iteration, my LIST has the whole dict inside, just sorted by key. But on the second iteration, when it sorts the first value of the first dict (at bookmark) - in this case the list of A-, and the list gets appended to LIST, it automaticly disappears in the dict in LIST
[{'A': None, 'B': {4: 'hallo', 0: [4, 4, 4, 5, 1], 1: {'b': 'Potato', 'a': ['b', 'd', 'c']}}, 'Z': 20}, [0, 1, 2, 2, 4, 6, 8]]
Sry for the strange description but i am really confused.
question from:
https://stackoverflow.com/questions/65626842/dict-values-disappear-while-sorting