Home > Mobile >  Dictionary items are not being all stored as expected
Dictionary items are not being all stored as expected

Time:11-05

I have a dictionary that is mapping one value to another value and it has multiple keys.

replace_dict={
    "key1":
        {"abc": "ABCDE", "XYZ": "WXYZ"},
    "key2":
        {"Alabama": "AL", "Alaska": "AK"}}

when I run the code

print(replace_dict.items())

the output is:

dict_items([('key1', {'abc': 'ABCDE', 'XYZ': 'WXYZ'}), ('key2', {'Alabama': 'AL', 'Alaska': 'AK'})])
{'abc': 'ABCDE', 'XYZ': 'WXYZ'}
{'abc': 'ABCDE', 'XYZ': 'WXYZ'}

I was expecting for it to look more like

dict_items([('key1', {'abc': 'ABCDE', 'XYZ': 'WXYZ'}), ('key2', {'Alabama': 'AL', 'Alaska': 'AK'})])
{'abc': 'ABCDE', 'XYZ': 'WXYZ'}
{'Alabama': 'AL', 'Alaska': 'AK'}

How do I correctly format the dictionary so the output is as desired?

CodePudding user response:

You can do the following formatting by using for loop in replace_dict.items()

Here are the few formatting examples

Example 1

for key, value in replace_dict.items():
    print(key)

Output:

key1
key2

Example 2

for key, value in replace_dict.items():
    print(value)

Output:

{'abc': 'ABCDE', 'XYZ': 'WXYZ'}
{'Alabama': 'AL', 'Alaska': 'AK'}

Example 3

for key, value in replace_dict.items():
    print(key, ' : ', value)

Output:

key1  :  {'abc': 'ABCDE', 'XYZ': 'WXYZ'}
key2  :  {'Alabama': 'AL', 'Alaska': 'AK'}

CodePudding user response:

I ran your code and printed and i go the output u were expecting...

  • Related