Dictionary comprehension

Write some python code that uses ‘for’ statement to iterate over a dictionary called ‘module_stats’ and print its value and keys? Please can some one help?

Python syntax is very friendly.

https://www.w3schools.com/python/gloss_python_loop_dictionary_items.asp

Try this website and post your code… if you have questions, I’m glad to help.

I will try thank u.
but do u have any idea about the solution of this

Zee, I’ve seen you have posted questions, and people are trying to help, but the most interesting part of programing is reading the documentation and then adapt it to your needs.

So, my suggestion: read the website I sent you, and analyze how the code works. Then invent a dictionary and try to make it work. At first is not easy, but once your brain takes the idea, there is no limit.

I could give you the answer to your question, but you wouldn’t learn, and I think you took Mosh course to learn… So, try it… watch Mosh video again, read the docs, post your code and people will help you if you show effort.

And remember: If it were easy, everybody would do it.

1 Like

Thank you for the suggestions nicolas. I have taken the course to build my knowledge or to help me better understand python.

However, if I upload a question, it doesn’t mean I haven’t tried or you might think I found the question and upload it here to get the solution. If I ask this means I want the process as how to do it, not what to do.

In addition, I have to mention that Every sites are not save that I jumped and starting working on them.

This is one of the solution where I ask previously. But I try to solve in my own way and of course I came up with the idea of those who were really nice and answer me with the solutions.

def findMin(a, b):
“””fine the smallest value.”””
if a < b:
min = a
else:
min = b
return min

a = 100
b = 200
find_min(100, 200)
100

Best wishes

but still thank you very much for taking the time and respond to me.

Great! We are on the same boat then!

And if you read the documentation, and watch Mosh videos you will learn HOW to do it. It´s there… that´s why we are all here!

Perhaps you are not asking the right question in the forum, then. Perhaps you want to compare your code with others, and if that´s the case, I think you have a fantastic idea. But asking how to iterete over a dictionary gives me the idea that you want people to write the code for you… but that´s just the feeling I have when I read your post and that´s why I replied as I did in this forum.

Why not. We can ask any questions even they are related to computer science, information technology, AI or anything else. Or even I can ask why people not scroll down without typing lol.
I come here to solve and build up my knowledge not stop asking because it doesn’t suites you lol

All the best in your programming path, Zee.

Thank you. Same to you

Here is an example of Dictionary Comprehension using a for statement to iterate over a dictionary called module_stats and print its key-value pairs:

module_stats = {'Math': 95, 'English': 87, 'History': 92}

for subject, marks in module_stats.items():
    print(f"Subject: {subject} | Marks: {marks}")

In this example, the for loop iterates over the items() of the module_stats dictionary, and the subject and marks variables are assigned the key and value of each key-value pair in the dictionary, respectively. The f"{}" string format is used to print the subject and marks, and the .items() method is used to return the key-value pairs in the dictionary as a list of tuples.