Python Forum

Full Version: Dictionaries in Lists
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi there!

I started learning Python with the help of "Python Crash Course" by Eric Matthes. The progress is good, but I stuck at one of the exercises and sadly, the book doesn't have solutions given away. I tried checking several tutorials before asking here, but no luck. Sad Here's the task:

Quote:Make several dictionaries. In each dictionary, include keys and values. Store these dictionaries in a list. Next, loop through your list
and as you do print everything you know about each dictionary.

I got this far:

name1 = {'key': 'value', 'key2': 'value2', }
name2 = {'key3': 'value3', 'key4': 'value4', }
name3 = {'key5': 'value5', 'key6': 'value6', }

names = [name1, name2, name3]

for name in names:
#?????
    for keys, values in name.items():
        print("\t" + keys + ":" + values)
The code prints out the keys and values just fine, but even after trying many alternatives, I can't print out the names of the dictionaries. I only manage to print out the whole dictionary. My goal for the print output is this:

name1:
    key:value
    key2:value2
name2:
    key3:value3
    key4:value4
name3:
    key5:value5
    key6:value6
Any help is much appreciated!
why do you think you should print also variable name?
(Nov-05-2019, 11:45 AM)buran Wrote: [ -> ]why do you think you should print also variable name?

I'm not sure I have to, but as I start to understand, you can basically do anything you want in programming and now that I decided to print out the dictionaries names, which became a problem for me to solve, I definitely want to. Big Grin
Maybe you can make use of this:
dictionaries = {
    "name1": {'key1': 'value1', 'key2': 'value2'},
    "name2": {'key3': 'value3', 'key4': 'value4'},
    "name3": {'key5': 'value5', 'key6': 'value6'}
}

for dictionary_name, subdictionaries in dictionaries.items():
    print(dictionary_name)
    for key, value in subdictionaries.items():
        print(f"\t{key}:{value}")
Output:
name1 key1:value1 key2:value2 name2 key3:value3 key4:value4 name3 key5:value5 key6:value6

The assignment specifically does not ask for printing name of dict
If OP wants to do this, it can be done with modified assignment. :-)
Quote:If OP wants to do this, it can be done with modified assignment. :-)

So maybe I made a much harder task out of it, than it originally was...? Big Grin
It's likely, because I can't really find a part in the chapter, that could help in this. I'm curious to know if later on this problem will be much easier to solve with - as ThomasL said - modified assignments. Smile
Hi, I'm the author of Python Crash Course. There are solutions available online, but not to this specific exercise. The online resources for the first edition are here: http://ehmatthes.github.io/pcc/

The resources for the second edition are here: https://ehmatthes.github.io/pcc_2e/
(Nov-06-2019, 03:36 PM)ehmatthes Wrote: [ -> ]Hi, I'm the author of Python Crash Course. There are solutions available online, but not to this specific exercise. The online resources for the first edition are here: http://ehmatthes.github.io/pcc/

The resources for the second edition are here: https://ehmatthes.github.io/pcc_2e/

Thank you for the links, they will definitely come handy in later chapters! Smile

By the way the book is really good, both content- and layout-wise; glad I can give a feedback to the author himself! Big Grin