How To Use Dictionaries In Python (Python Tutorial #8)

CS Dojo · Beginner ·🛠️ AI Tools & Apps ·8y ago

Key Takeaways

This video tutorial demonstrates how to use dictionaries in Python, covering topics such as creating dictionaries, adding key-value pairs, retrieving values, and iterating over key-value pairs. The tutorial also highlights the importance of using Google to find solutions to programming problems.

Full Transcript

in this video I'm going to show you how to use dictionaries in Python you can think of a dictionary in Python as sort of like a lookup table just as an example let's say you have this table of information this table indicates that George is 24 years old tom is 32 years old and Jenny is 16 or in general there's the value of 24 with the key George the string and the value that's associated with the key Tom the string is 32 these are sometimes called key value pairs because we have a bunch of keys and a value associated with each of those keys and you can use a dictionary to store this kind of information so let's see how we can do that in Python code to create a new dictionary you can write the following D equals curly brackets and when you run this cell this defines a new dictionary that's empty so it doesn't have any keys or values yet and it assigns that dictionary to the new variable D another way to write the same thing is to write D equals dict parentheses and it would do exactly the same thing but let's go with curly brackets for now and actually you could also write this so that it has some predefined keys and values as well so for that you can write D equals curly brackets double quotes George : 24 comma double quotes Tom : 32 and this defines a new dictionary with the value 24 that's associated with the key George and the value is 32 that's associated with the key Tom but let's go with the first one for now to define an empty dictionary after defining an empty dictionary you can add a key value pair like so these square brackets double quotes George equals 24 this says add a new key value pair where the key is George and the value is 24 we can more pairs by writing for example D square brackets double quotes tom equals 32 and the square brackets Jenny equals 16 and now this dictionary has the three key value pairs and to find a value that's associated with a certain key for example George you can just write print the square brackets George and this will print out the value that's associated with the value George and then of course if you want to print out the value that's associated with the key Tom you can just write print these square brackets Tom and then it will print out 32 and if you try to look up a key that doesn't exist yet it's gonna give us an error for example if you do the scar brackets Alice it gives us this key error because the key Alice doesn't exist yet now as we saw the value that associated with Jenny was 16 but it's possible to change the value that's associated with a certain key after we already have that key value pair in the dictionary we can do that with just like before the square brackets Jenny equals 20 and once we run this cell now the value that's associated with Jenny is 20 now one thing to note here is that the values can be any type but the keys can only be certain types and there are different types we can use for that but most commonly keys are strings or numbers and in Python you can even mix different types of keys so for example you can add a new key let's say 10 with the value 102 this dictionary D with D square brackets 10 equals 100 and once we write print parentheses the square brackets 10 it works just fine retrieving the value 100 now what if we want it to iterate over key value pairs in the given dictionary for example this one D let's pretend for a second that we don't know how to do that and of course if we don't know how to do something like that the first thing we should do is we should just use Google and I'm gonna show you how I would go about it and for that I might write something like how to iterate over key value pairs dictionary Python let's take a look at the first result this one right here from Stack Overflow and just be careful because this one is a little bit old it's from 2010 but it might be okay and you can see that this question is about a dictionary and then a for loop here so I would just skim through this page and then I would try to find what I'm looking for which is something about Python 3 probably so it says for Python 3 points X we're using Python 3 by the way it looks like we might be able to use four key comma value in d items and it looks like this D is the dictionary so let's see if it works by copying this over here and let's try print key and then key without the double quotes and then the same thing with the value it's a print value and then value and once we run this cell we see key is George and then the value that's associated with it is 24 and to make this look a little nicer let's add colons here and let's add an empty line here by adding an empty string here with the print function and when we run this cell again it looks much nicer so value is 24 for the key George and the value that's associated with Tom is 32 Jennie 20 and the value that's associated with the key 10 is 100 so it looks like this method works okay and that's the basics of how to use dictionaries in Python and actually the ability to use Google just like I did to find what you're looking for when you don't know how to do certain tasks is an important skill to have as a software developer as I talked about it in my last video so that's definitely something I would cultivate over time if you're just getting started with programming anyway I will see you guys in the next video

Original Description

This entire series in a playlist: https://goo.gl/eVauVX Download the sample file: https://www.csdojo.io/python8 Keep in touch on Facebook: https://www.facebook.com/entercsdojo Support me on Patreon: https://www.patreon.com/csdojo
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from CS Dojo · CS Dojo · 39 of 60

1 4 Hacks for Finding the Optimal Answer in Coding Interview QUICKLY!
4 Hacks for Finding the Optimal Answer in Coding Interview QUICKLY!
CS Dojo
2 Dynamic Programming Tutorial with Fibonacci Sequence
Dynamic Programming Tutorial with Fibonacci Sequence
CS Dojo
3 Kadane's Algorithm to Maximum Sum Subarray Problem
Kadane's Algorithm to Maximum Sum Subarray Problem
CS Dojo
4 Longest Common Subsequence (Dynamic Programming)
Longest Common Subsequence (Dynamic Programming)
CS Dojo
5 0-1 Knapsack Problem (Dynamic Programming)
0-1 Knapsack Problem (Dynamic Programming)
CS Dojo
6 Amazon Coding Interview: Count Negative Integers in Row/Column-Wise Sorted Matrix
Amazon Coding Interview: Count Negative Integers in Row/Column-Wise Sorted Matrix
CS Dojo
7 Microsoft Coding Interview Question and Answer: Lowest Common Ancestor
Microsoft Coding Interview Question and Answer: Lowest Common Ancestor
CS Dojo
8 Learn Counting Sort Algorithm in LESS THAN 6 MINUTES!
Learn Counting Sort Algorithm in LESS THAN 6 MINUTES!
CS Dojo
9 Radix Sort Algorithm Introduction in 5 Minutes
Radix Sort Algorithm Introduction in 5 Minutes
CS Dojo
10 Coding Interview Question and Answer: Longest Consecutive Characters
Coding Interview Question and Answer: Longest Consecutive Characters
CS Dojo
11 Coding Interview: Can You RANDOMLY Reorder Array in O(N)?
Coding Interview: Can You RANDOMLY Reorder Array in O(N)?
CS Dojo
12 Coding Interview Question: Tower Hopper Problem
Coding Interview Question: Tower Hopper Problem
CS Dojo
13 Problem Solving Technique #1 for Coding Interviews with Google, Amazon, Microsoft, Facebook, etc.
Problem Solving Technique #1 for Coding Interviews with Google, Amazon, Microsoft, Facebook, etc.
CS Dojo
14 Google Coding Interview Question and Answer #1: First Recurring Character
Google Coding Interview Question and Answer #1: First Recurring Character
CS Dojo
15 Facebook Coding Interview Question and Answer #1: All Subsets of a Set
Facebook Coding Interview Question and Answer #1: All Subsets of a Set
CS Dojo
16 Think you're not smart enough to work at Google? Well, think again.
Think you're not smart enough to work at Google? Well, think again.
CS Dojo
17 How to Crack a Google Coding Interview - An Ex-Googler’s Guide
How to Crack a Google Coding Interview - An Ex-Googler’s Guide
CS Dojo
18 Amazon Coding Interview Question - K Closest Points to the Origin
Amazon Coding Interview Question - K Closest Points to the Origin
CS Dojo
19 How I Got an Internship at Microsoft
How I Got an Internship at Microsoft
CS Dojo
20 How I Got a Job at Google as a Software Engineer (without a Computer Science Degree!)
How I Got a Job at Google as a Software Engineer (without a Computer Science Degree!)
CS Dojo
21 Why I Left My $100,000+ Job at Google
Why I Left My $100,000+ Job at Google
CS Dojo
22 Top 5 Programming Languages to Learn to Get a Job at Google, Facebook, Microsoft, etc.
Top 5 Programming Languages to Learn to Get a Job at Google, Facebook, Microsoft, etc.
CS Dojo
23 How I Learned to Code - and Got a Job at Google!
How I Learned to Code - and Got a Job at Google!
CS Dojo
24 Why I Left Google To Be A YouTuber FULL-TIME (and NOT part-time!)
Why I Left Google To Be A YouTuber FULL-TIME (and NOT part-time!)
CS Dojo
25 What Is Dynamic Programming and How To Use It
What Is Dynamic Programming and How To Use It
CS Dojo
26 Python Tutorial for Absolute Beginners #1 - What Are Variables?
Python Tutorial for Absolute Beginners #1 - What Are Variables?
CS Dojo
27 What's It Really Like To Intern At Google? (LIVE with a former Google software engineer intern)
What's It Really Like To Intern At Google? (LIVE with a former Google software engineer intern)
CS Dojo
28 How to Use If Else Statements in Python (Python Tutorial #2)
How to Use If Else Statements in Python (Python Tutorial #2)
CS Dojo
29 Dynamic Programming Interview Question #1 - Find Sets Of Numbers That Add Up To 16
Dynamic Programming Interview Question #1 - Find Sets Of Numbers That Add Up To 16
CS Dojo
30 How To Use Functions In Python (Python Tutorial #3)
How To Use Functions In Python (Python Tutorial #3)
CS Dojo
31 What’s It Like To Be A Program Manager Intern At Microsoft? (LIVE with a former Microsoft intern)
What’s It Like To Be A Program Manager Intern At Microsoft? (LIVE with a former Microsoft intern)
CS Dojo
32 Introduction To Lists In Python (Python Tutorial #4)
Introduction To Lists In Python (Python Tutorial #4)
CS Dojo
33 Introduction to For Loops in Python (Python Tutorial #5)
Introduction to For Loops in Python (Python Tutorial #5)
CS Dojo
34 What Programming Language Should I Learn First?
What Programming Language Should I Learn First?
CS Dojo
35 What Is Competitive Programming and How To Prepare For It (LIVE with Gaurav Sen)
What Is Competitive Programming and How To Prepare For It (LIVE with Gaurav Sen)
CS Dojo
36 While Loops and The Break Statement in Python (Python Tutorial #6)
While Loops and The Break Statement in Python (Python Tutorial #6)
CS Dojo
37 More About For Loops in Python & Solutions to the Last 2 Problems (Python Tutorial #7)
More About For Loops in Python & Solutions to the Last 2 Problems (Python Tutorial #7)
CS Dojo
38 How to Learn to Code - Best Resources, How to Choose a Project, and more!
How to Learn to Code - Best Resources, How to Choose a Project, and more!
CS Dojo
How To Use Dictionaries In Python (Python Tutorial #8)
How To Use Dictionaries In Python (Python Tutorial #8)
CS Dojo
40 Data Structures & Algorithms #1 - What Are Data Structures?
Data Structures & Algorithms #1 - What Are Data Structures?
CS Dojo
41 An Overview of Arrays and Memory (Data Structures & Algorithms #2)
An Overview of Arrays and Memory (Data Structures & Algorithms #2)
CS Dojo
42 Introduction to Classes and Objects - Part 1 (Data Structures & Algorithms #3)
Introduction to Classes and Objects - Part 1 (Data Structures & Algorithms #3)
CS Dojo
43 Classes and Objects with Python - Part 1 (Python Tutorial #9)
Classes and Objects with Python - Part 1 (Python Tutorial #9)
CS Dojo
44 Introduction to Classes and Objects - Part 2 (Data Structures & Algorithms #4)
Introduction to Classes and Objects - Part 2 (Data Structures & Algorithms #4)
CS Dojo
45 Classes and Objects with Python - Part 2 (Python Tutorial #10)
Classes and Objects with Python - Part 2 (Python Tutorial #10)
CS Dojo
46 Introduction to Linked Lists (Data Structures & Algorithms #5)
Introduction to Linked Lists (Data Structures & Algorithms #5)
CS Dojo
47 Introduction to Recursion (Data Structures & Algorithms #6)
Introduction to Recursion (Data Structures & Algorithms #6)
CS Dojo
48 Introduction to Big O Notation and Time Complexity (Data Structures & Algorithms #7)
Introduction to Big O Notation and Time Complexity (Data Structures & Algorithms #7)
CS Dojo
49 Amazon Coding Interview Question - Recursive Staircase Problem
Amazon Coding Interview Question - Recursive Staircase Problem
CS Dojo
50 Using Boolean in Python (Python Tutorial #11)
Using Boolean in Python (Python Tutorial #11)
CS Dojo
51 Intro to Data Analysis / Visualization with Python, Matplotlib and Pandas | Matplotlib Tutorial
Intro to Data Analysis / Visualization with Python, Matplotlib and Pandas | Matplotlib Tutorial
CS Dojo
52 What Can You Do with Python? - The 3 Main Applications
What Can You Do with Python? - The 3 Main Applications
CS Dojo
53 Facebook Coding Interview Question - How Many Ways to Decode This Message?
Facebook Coding Interview Question - How Many Ways to Decode This Message?
CS Dojo
54 List Comprehension Basics with Python (Python Tutorial #12)
List Comprehension Basics with Python (Python Tutorial #12)
CS Dojo
55 How To Use Sets in Python (Python Tutorial #13)
How To Use Sets in Python (Python Tutorial #13)
CS Dojo
56 Python books for beginners? What Python projects to work on? | 2 Python Beginner FAQ’s!
Python books for beginners? What Python projects to work on? | 2 Python Beginner FAQ’s!
CS Dojo
57 Resources for Learning Data Structures and Algorithms (Data Structures & Algorithms #8)
Resources for Learning Data Structures and Algorithms (Data Structures & Algorithms #8)
CS Dojo
58 6 Python Exercise Problems for Beginners - from CodingBat (Python Tutorial #14)
6 Python Exercise Problems for Beginners - from CodingBat (Python Tutorial #14)
CS Dojo
59 Google Coding Interview - Universal Value Tree Problem
Google Coding Interview - Universal Value Tree Problem
CS Dojo
60 Best laptops for programming? How to get a job at Google? - And other FAQ’s!
Best laptops for programming? How to get a job at Google? - And other FAQ’s!
CS Dojo

This video tutorial teaches the basics of using dictionaries in Python, including creation, manipulation, and iteration. It also emphasizes the importance of using Google to find solutions to programming problems.

Key Takeaways
  1. Create an empty dictionary using curly brackets or the dict() function
  2. Add key-value pairs to a dictionary using square brackets
  3. Retrieve values from a dictionary using square brackets
  4. Iterate over key-value pairs in a dictionary using a for loop and the items() method
  5. Use Google to find solutions to programming problems
💡 Dictionaries are a fundamental data structure in Python, and understanding how to use them is crucial for any programmer. Additionally, developing the skill to use Google effectively can save time and increase productivity.

Related Reads

📰
What If Your “New Applicant” Isn’t Actually New?
Learn to identify and handle duplicate job applicants using AI-powered tools and strategies to optimize your hiring process
Medium · AI
📰
I Tested deepseek-harness — Here's What You Need to Know
Learn about deepseek-harness, an open-source project with industry-leading performance and privacy-focused features, and how to get started with it
Dev.to AI
📰
The Best AI Checker Tools of 2026, Tested on the Same 16 Samples
Discover the top AI checker tools of 2026, tested for accuracy and reliability to help you make informed decisions
Medium · AI
📰
ScrollEd Wants to Turn Infinite Scrolling Into a Learning Tool
ScrollEd uses AI to turn textbooks into a swipeable learning feed, leveraging infinite scrolling for education
TechRepublic
Up next
How to Get Your Brand Cited by ChatGPT, Claude and Gemini
Arvow
Watch →