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