Dictionary in Python

Step-by-step Python programming tutorial

Master the art of using Python dictionaries in this insightful video tutorial. Learn how to initialize, populate, and update dictionaries, and how they differ from other Python data types like integers, floats, strings, and lists.

Key Insights

  • The video tutorial provides detailed information on how to handle dictionaries in Python, a built-in data type different from integers, floats, strings, and lists.
  • To initialize a dictionary, a variable name should be chosen and followed by the use of curly braces.
  • The 'type' function can be used to check if the initialized variable is a dictionary.
  • A dictionary is a collection of items, each having a unique key and a value. The key and value are separated by a colon, and different items are separated by a comma.
  • To update values in a dictionary, the key should be used and a new value assigned.
  • The 'keys' function is used to view all keys, and the key within square brackets is used to fetch a value or update a value.

Python Workflow

  1. Set Up venv

    python -m venv .venv — isolated environment per project.

  2. Install Dependencies

    pip install -r requirements.txt — pin versions for reproducibility.

  3. Write & Test

    Write functions; test with pytest as you go.

  4. Run & Deploy

    python script.py locally, deploy to your platform of choice.

Play video

Video Transcription

Hi, My Name is Art and I Teach Python at Noble Desktop. in This Video, I'll Explain Dictionaries to You. Remember That Python Has Built-in Data Types Like Integers, Floats, Strings, and Lists. How Do You Initialize a Dictionary?

First, come up with a variable name, like 'd', and use curly braces. To check if it's a dictionary, use the 'type' function and it should return 'dictionary'.

Before I give you a formal definition of a dictionary, let me show you how we can populate the dictionary with data. Suppose this is my phone book, and I have a friend named John. John is the key, and 'Manhattan' is the value. We can add another friend, Mary, her phone number, and also Mark and his phone number.

Python for Data Science Bootcamp: Live & Hands-on, In NYC or Online, Learn From Experts, Free Retake, Small Class Sizes,  1-on-1 Bonus Training. Named a Top Bootcamp by Forbes, Fortune, & Time Out. Noble Desktop. Learn More.

A dictionary is a completely different collection than what we've seen so far—it is a collection of items, where each item has a key and a value, separated by a colon and each item separated by a comma. Keys must be unique.

It's easy to update values—just use the key and assign a new value. To see all keys, use the 'keys' function. To fetch a value, use the key in square brackets. To update a value, use the key in square brackets and assign a new value.

Watch my other videos to learn how to use a 'for' loop to iterate through a dictionary.

Topics

Learn more in these courses

How to Learn Python

Master Python with hands-on training. Python is a popular object-oriented programming language used for data science, machine learning, and web development. 

Yelp Facebook LinkedIn YouTube Twitter Instagram