Check if Key Exists

To determine if a specified key is present in a dictionary use the in keyword:

Example

Check if "model" is present in the dictionary:

thisdict = {

  "brand": "Ford",

  "model": "Mustang",

  "year": 1964

}

if "model" in thisdict:

 print("Yes, 'model' is one of the keys in the thisdict dictionary")

Dictionary Length

To determine how many items (key-value pairs) a dictionary has, use the len() method.

Example

Print the number of items in the dictionary:

print(len(thisdict))

Adding Items

Adding an item to the dictionary is done by using a new index key and assigning a value to it:

Example

thisdict = {

  "brand": "Ford",

  "model": "Mustang",

  "year": 1964

}

thisdict["color"] = "red"

print(thisdict)

Jay Kakadiya

Jay Kakadiya Creator

I am a computer field, & i am Web developer.

Suggested Creators

Jay Kakadiya