Skip to main content

Put a key name inside square brackets to read the matching item from a dictionary:

Example

Read the value stored under "model":

Prompt
Write a Python program that creates a dictionary called thisdict with keys "brand", "model", and "year" with values "Ford", "Mustang", and 1964. Then get and print the value of the "model" key using square bracket notation.
Output: complete Python program

Get Keys

Calling keys() gives you a view of every key in the dictionary, which reads like a list.

Example

Retrieve all the keys:

Prompt
Write a Python program that creates a dictionary called thisdict with keys "brand", "model", and "year" with values "Ford", "Mustang", and 1964. Then get and print the keys of the dictionary using the keys() method.
Output: complete Python program

Example

The keys view stays current: add an item to the dictionary and it shows up there as well:

Prompt
Write a Python program that creates a dictionary called car with keys "brand", "model", and "year" with values "Ford", "Mustang", and 1964. Store the keys() view in variable x, print x, then add a new key "color" with value "white" to car, and print x again to show it updates.
Output: complete Python program

Get Values

Calling values() works the same way, giving you a list-like view of every value in the dictionary.

Example

Retrieve all the values:

Prompt
Write a Python program that creates a dictionary called thisdict with keys "brand", "model", and "year" with values "Ford", "Mustang", and 1964. Then get and print the values of the dictionary using the values() method.
Output: complete Python program

Example

Change a value in the dictionary and the values view changes with it:

Prompt
Write a Python program that creates a dictionary called car with keys "brand", "model", and "year" with values "Ford", "Mustang", and 1964. Store the values() view in variable x, print x, then change car["year"] to 2020, and print x again to show it updates.
Output: complete Python program

Example

Add an item to the dictionary and its value appears in the values view too:

Prompt
Write a Python program that creates a dictionary called car with keys "brand", "model", and "year" with values "Ford", "Mustang", and 1964. Store the values() view in variable x, print x, then add a new key "color" with value "white", and print x again.
Output: complete Python program

Get Items

Calling items() returns a list-like view holding each dictionary item as a tuple.

Example

Retrieve all the key:value pairs:

Prompt
Write a Python program that creates a dictionary called thisdict with keys "brand", "model", and "year" with values "Ford", "Mustang", and 1964. Then get and print the items of the dictionary using the items() method.
Output: complete Python program

Example

Change a value in the dictionary and the items view changes with it:

Prompt
Write a Python program that creates a dictionary called car with keys "brand", "model", and "year" with values "Ford", "Mustang", and 1964. Store the items() view in variable x, print x, then change car["year"] to 2020, and print x again.
Output: complete Python program

Check if Key Exists

The in keyword tells you whether a given key exists in a dictionary:

Example

Find out whether "model" is one of the keys:

Prompt
Write a Python program that creates a dictionary called thisdict with keys "brand", "model", and "year" with values "Ford", "Mustang", and 1964. Then check if "model" is present in the dictionary using the in keyword, and if so, print "Yes, 'model' is one of the keys in the thisdict dictionary".
Output: complete Python program

The get() Method

Square brackets are not your only choice for reading a value. The get() method returns the same result:

Example

Use get() to read the value stored under "model":

Prompt
Write a Python program that creates a dictionary called thisdict with keys "brand", "model", and "year" with values "Ford", "Mustang", and 1964. Then get and print the value of the "model" key using the get() method.
Output: complete Python program