Dictionaries in Python

Buildandteach: Henry Palma
2 min readFeb 17, 2022

--

Features of a Dictionary

A dictionary is a collection of objects. It contains the following features:

  • It is unordered
  • It is changeable
  • It is indexed
  • Uses key value pairs

1) Create a Dictionary

Dictionaries are created with an empty pair of curly brackets.

2) Accessing items in a Dictionary

You can access items using the key or use the get() method or use the key inside of square brackets.

3) Change a value in a Dictionary

Use the key to change a value in the dictionary.

4) Loop through a Dictionary and access the keys in the Dictionary

Use a for loop to iterate through the keys of a dictionary.

5) Loop through a Dictionary and access the values in the Dictionary

To loop through the values of a Dictionary use a for loop and put the looping value inside of square brackets:

6) Loop through a Dictionary and access the values and keys in the Dictionary

To loop through the values of a Dictionary use a for loop with 2 looping values:

7) Check if a value exists in a Dictionary

Use the “in” keyword to check if a value exists in a Dictionary.

8) Get the length of a Dictionary

Use the len() method to get the size/length of a Dictionary.

9) Remove an item from a Dictionary using a key

Use the pop method to remove an item from a Dictionary.

10) Clear all items from a Dictionary.

Use the clear() method to clear all items from a dictionary

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response