Accessing, editing and looping through dictionary items

Dictionaries in Python are a collection of key-value pairs — meaning every item in the dictionary has a key and an associated value.

If we want to write down prices of some items in a grocery store, normally we will note them on a piece of paper like this:

eggs - 4.99
banana - 1.49
cheese- 4.5
eggplant - 2.5
bread - 3.99

In Python dictionary lingo, the name of each item is “key” and the associated price is “value” and they appear in pairs. We can represent the same in a Python dictionary data structure as follows:

{"eggs": 4.99,
"banana": 1.49,
"cheese": 4.5,
"eggplant": 2.5,
"bread": 3.99}

Notice the differences. In the dictionary

  • each key is within quotation marks because they are strings
  • the associated values are not quoted because they are numeric
  • keys and values are separated by a colon (:)
  • the items are comma-separated

#dictionary #python #artificial-intelligence #dictionaries #python dictionary #working with python dictionaries

Working with Python dictionaries: a cheat sheet
1.60 GEEK