Dictionaries are the main mapping type that we’ll use in Python. This object is similar to a map in Java.

In Python, we create dictionary literals by using curly braces {}, separating keys from values using colons :, and separating key/value pairs using commas ,.

How to Create a Dictionary

datedict = {
  "date": 13,
  "month": "January",
  "year": 1970
}
print(datedict)

Output:

{'date': 13, 'month': 'January', 'year': 1970}

How to Access Dictionary Items

We can access dictionary items by referring to the key name inside square brackets [] or using the get() method:

datedict = {
  "date": 13,
  "month": "January",
  "year": 1970
}
date = datedict["date"]
year = datedict.get("year")

print(date, year)

Output:

13 1970

How to Change an Item’s Value

We can change the value of an item in the dictionary by referring to its key name, for example:

datedict = {
  "date": 13,
  "month": "January",
  "year": 1970
}
datedict["date"] = 20
print(datedict)

Output:

{'date': 20, 'month': 'January', 'year': 1970}

How to Loop Through a Dictionary

We can loop through a dictionary using the for loop. When looping through a dictionary, we can print all the keys, all the values or all the key/value pairs:

Get All Dictionary Keys

datedict = {
  "date": 13,
  "month": "January",
  "year": 1970
}
for d in datedict:
    print(d)

Output:

date
month
year

Get All Dictionary Values

datedict = {
  "date": 13,
  "month": "January",
  "year": 1970
}
for d in datedict:
    print(datedict[d])

Output:

13
January
1970

We can also use the values() function to return the values:

datedict = {
  "date": 13,
  "month": "January",
  "year": 1970
}
for d in datedict.values():
    print(d)

Output:

13
January
1970

Get Both Keys and Values in Dictionary

We can use the items() function to print keys and values:

datedict = {
  "date": 13,
  "month": "January",
  "year": 1970
}
for k, v in datedict.items():
    print(k, v)

Output:

date 13
month January
year 1970

How to Get the Length of a Dictionary

You can get the dictionary length (number of key/value pairs) by calling the len() function, e.g.:

datedict = {
  "date": 13,
  "month": "January",
  "year": 1970
}
print(len(datedict))

Output:

3

How to Add Items to a Dictionary

To add a key/value pair to a dictionary we need to provide a new key and an associated value. For example:

datedict = {
  "date": 13,
  "month": "January",
  "year": 1970
}
datedict["season"] = "winter"
print(datedict)

Output:

{'date': 13, 'month': 'January', 'year': 1970, 'season': 'winter'}

How to Remove Items From a Dictionary

To remove an item from a dictionary, supply the key name to the pop() method.

datedict = {
  "date": 13,
  "month": "January",
  "year": 1970,
  "season": "winter"
}
datedict.pop("season")
print(datedict)

Output:

{'date': 13, 'month': 'January', 'year': 1970}

We can also use the del keyword to delete an item with a specified key:

datedict = {
  "date": 13,
  "month": "January",
  "year": 1970,
  "season": "winter"
}
del datedict["season"]
print(datedict)

Output:

{'date': 13, 'month': 'January', 'year': 1970}
datedict = {
  "date": 13,
  "month": "January",
  "year": 1970,
}
del datedict

How to Empty the Dictionary

To empty the dictionary from all the key/value pairs, use the clear() method

datedict = {
  "date": 13,
  "month": "January",
  "year": 1970,
  "season": "winter"
}
datedict.clear()
print(datedict)

Output:

{}
See also