Python Forum
Dictionaries in Lists
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dictionaries in Lists
#1
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!
Reply
#2
why do you think you should print also variable name?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(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
Reply
#4
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. :-)
Reply
#5
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
Reply
#6
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/
Reply
#7
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  QUERY on Looping and creating lists as items within dictionaries ajayachander 3 2,325 Mar-26-2020, 02:03 PM
Last Post: ajayachander
  Help with Dictionaries and Lists of Lists Nate5800 5 74,906 Apr-23-2018, 06:42 PM
Last Post: Nate5800

Forum Jump:

User Panel Messages

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