Dictionaries in Lists - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Dictionaries in Lists (/thread-22243.html) |
Dictionaries in Lists - szbeco - Nov-05-2019 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. 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 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:value6Any help is much appreciated! RE: Dictionaries in Lists - buran - Nov-05-2019 why do you think you should print also variable name? RE: Dictionaries in Lists - szbeco - Nov-05-2019 (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. RE: Dictionaries in Lists - ThomasL - Nov-05-2019 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}")
The assignment specifically does not ask for printing name of dict If OP wants to do this, it can be done with modified assignment. :-) RE: Dictionaries in Lists - szbeco - Nov-05-2019 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...? 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. RE: Dictionaries in Lists - ehmatthes - Nov-06-2019 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/ RE: Dictionaries in Lists - szbeco - Nov-07-2019 (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/ Thank you for the links, they will definitely come handy in later chapters! By the way the book is really good, both content- and layout-wise; glad I can give a feedback to the author himself! |