-1

I recall reading that from Python 3.6+ dictionaries would be ordered so I figured that it would hold true for updating/merging dictionaries as well. Unfortunately when I run my code, my output has a jumbled mess of key:value pairs while I thought I would keep the order and just append onto the back of the dictionary like list.append().

Originally I was using the dictonary1.update({dict2_key:dict2:val}), but I've also tried the geeksforgeeks merge as well as kwargs option, but it's still giving me a jumbled mess. I can't use the 3.9 union "dict1|dict2" as my work interpreter is 3.8 and updating is a whole mess.

Any resources I could look into to getting my dictionaries to just...tack on at the end? Or would that involve making them all lists, appending them, and then reverting it back to a dictionary again?

Edit: Anonymized sample of my dictionary outputs:

result = [{dict1},{dict2},{dict3},{dict4:[dict5,dict6]}]

output = {}

for item in result:
    for key in item.keys():
        if key == dict4["key"]:
            pass
        else:
            output[key] = item[key]
    for parameter in item["dict4[key]"]:
        function(parameter) #[changes the string of the keys][4]#using andidogs python 3 version 
        output = {**output,**parameter}
        
    print(output, "\n")
        
    print(output, "\n")
    
#1 record of output:
{#first item of dict1, #contents dict5, #contents of dict6, #second item of dict1, #contents of dict2}

#desired output:
#{#contents of dict1, #contents of dict2, contents of dict3, contents of dict5, contents of dict6}

Essentially I'm trying to unpack the contents of {dict4}, item by item and append it onto the end of the output dictionary so that the final {output} = fully flattened "key":"value" pairs. Some dicts have nested strings, some dicts have nested dicts.

Once that code chunk is done, I have to unpack another group of nested dictionaries but I should be able to extrapolate from here.

ddejohn
  • 8,775
  • 3
  • 17
  • 30
  • 1
    What do you mean "jumbled mess"? It's not easy to help if you give us nothing to work with. Please provide an [MRE](https://stackoverflow.com/help/minimal-reproducible-example). – ddejohn Sep 06 '21 at 05:50
  • Dictionaries are only 'insertion ordered'; it's not clear how you would define an insertion order comparison for elements of two different dictionaries, unless you mean something else. – kcsquared Sep 06 '21 at 06:35

1 Answers1

1

Here are two dictionaries:

In [1]: d1 = {str(x): x**2 for x in range(5)}

In [2]: d2 = {str(x): x**2 for x in range(5, 10)}

In [3]: d1
Out[3]: {'0': 0, '1': 1, '2': 4, '3': 9, '4': 16}

In [4]: d2
Out[4]: {'5': 25, '6': 36, '7': 49, '8': 64, '9': 81}

To combine the key:value pairs from d1 and d2, in that order, you can simply unpack both into a new dictionary:

In [5]: d3 = {**d1, **d2}

In [6]: d3
Out[6]:
{'0': 0,
 '1': 1,
 '2': 4,
 '3': 9,
 '4': 16,
 '5': 25,
 '6': 36,
 '7': 49,
 '8': 64,
 '9': 81}

Your post isn't at all clear about what you're expecting, and what exactly it is that you're calling a "jumbled mess".

ddejohn
  • 8,775
  • 3
  • 17
  • 30
  • Hi thanks for the feedback. I've gone an edited my post to try and make it a bit clearer. The issue is, that I've tried the **kwargs method you've illustrated above but the out put is the same as the other attempts. – snicksnackpaddywhack91 Sep 06 '21 at 14:43