Python Docs

Python Dictionaries — Full Cheat Sheet

A dictionary stores data in key-value pairs. Dictionaries are:

  • Ordered (Python 3.7+)
  • Mutable / Changeable
  • No duplicate keys allowed

Create a Dictionary

Dictionaries are written using curly braces with key-value pairs.

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
print(thisdict)

Access Dictionary Items

Use the key name inside square brackets.

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
print(thisdict["brand"])

Duplicate Keys

Duplicate keys overwrite the earlier value.

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964,
  "year": 2020
}
print(thisdict)

Dictionary Length

Use len() to count items.

print(len(thisdict))

Values Can Be Any Data Type

thisdict = {
  "brand": "Ford",
  "electric": False,
  "year": 1964,
  "colors": ["red", "white", "blue"]
}

Check Dictionary Type

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
print(type(thisdict))

Using dict() Constructor

thisdict = dict(name="John", age=36, country="Norway")
print(thisdict)

Access With Methods

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
x = thisdict.get("model")
thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
x = thisdict.keys()
thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
x = thisdict.values()
thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
x = thisdict.items()
thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
if "model" in thisdict:
  print("Yes")

Modify Dictionary Items

Change values by reassigning to their key.

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
thisdict["year"] = 2018
thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
thisdict.update({"year": 2020})

Add New Items

Use a new key name or update().

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
thisdict["color"] = "red"
thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
thisdict.update({"color": "red"})

Remove Items

Dictionaries provide many removal methods.

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
thisdict.pop("model")
print(thisdict)
thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
thisdict.popitem()
print(thisdict)
thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
del thisdict["model"]
print(thisdict)
thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
del thisdict
thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
thisdict.clear()
print(thisdict)

Loop Through Dictionary

You can loop through keys, values, or pairs.

for x in thisdict:
  print(x)
for x in thisdict:
  print(thisdict[x])
for value in thisdict.values():
  print(value)
for key in thisdict.keys():
  print(key)
for key, value in thisdict.items():
  print(key, value)

Copy a Dictionary

Use copy() or dict() to avoid reference issues.

mydict = thisdict.copy()
print(mydict)
mydict = dict(thisdict)
print(mydict)

Nested Dictionaries

Dictionaries can contain dictionaries.

myfamily = {
  "child1": {
    "name": "Emil",
    "year": 2004
  },
  "child2": {
    "name": "Tobias",
    "year": 2007
  },
  "child3": {
    "name": "Linus",
    "year": 2011
  }
}

Access Nested Item

print(myfamily["child2"]["name"])

Dictionary Methods

MethodDescription
clear()Removes all dictionary elements
copy()Returns a shallow copy
fromkeys()Creates a new dictionary from keys
get()Returns the value of a key
items()Returns dictionary items (key,value)
keys()Returns dictionary keys
pop()Removes the specified key
popitem()Removes last inserted item
setdefault()Returns value; inserts if not exists
update()Updates dictionary with pairs
values()Returns all values