0
import random

def main():

    Sobeys = {'broccoli': 1.27, 'muffins': 5.0, 'donuts': 1.85,
          'yogurt': 2.41, 'bagels': 4.91, 'bread': 4.48,
          'cupcakes': 3.54}

    print("Sobeys prices:")
    display(Sobeys)

def display(grocery_dict):
    print (grocery_dict)
    return

main()

How can I have this print everything in the dictionary line by line?

Yokoe
  • 1
  • 2
  • 1
    Does this answer your question? [How to print a dictionary line by line in Python?](https://stackoverflow.com/questions/15785719/how-to-print-a-dictionary-line-by-line-in-python) – Mr. T Jan 13 '21 at 18:36

2 Answers2

0

Try this:

Sobeys = {'broccoli': 1.27, 'muffins': 5.0, 'donuts': 1.85,
'yogurt': 2.41, 'bagels': 4.91, 'bread': 4.48, 'cupcakes': 3.54}

for item in Sobeys:
    print(item+" costs $"+str(Sobeys[item]))

This will iterate over the dictionary, printing the name of each item and how much it costs. You can customize it by changing the print statement.

Lakshya Raj
  • 1,669
  • 3
  • 10
  • 34
0

You can use the items() method and for loop to iterate over the key, value pairs in a dictionary.

Sobeys = {'broccoli': 1.27, 'muffins': 5.0, 'donuts': 1.85,
          'yogurt': 2.41, 'bagels': 4.91, 'bread': 4.48,
          'cupcakes': 3.54}

for k,v in Sobeys.items():
    print(f"'{k}' \t: {v}")
Anaam
  • 124
  • 6