Dictionaries – Key-Value Pairs (Python)
Video Title: Python Tutorial for Beginners 5: Dictionaries – Working with Key-Value Pairs
Hey there how’s it going everybody in this video we’ll be learning about dictionaries and how we can work with them in Python so dictionaries allow us to work with key value Pairs and if you’re coming from another programming language then you may have heard these called hashmaps or associative arrays so
When I say that we’ll be working with key value pairs these are two linked values where the key is a unique identifier where we can find our data and the value is that data so we can actually think of that almost like a real physical dictionary where we look
Up word definitions so in that example each word that we look up would be the key and the definition of that word would be the value so let’s go ahead and take a look at some examples so let’s say that we wanted to represent a student using a dictionary so to do this
We could just create a student variable and set this equal to these curly braces and that’s how we start our dictionary now within our curly braces here we will first put in our key so let’s say that we want a a key of name now we’re going
To put in a colon to separate our key from our value and for the name here we’ll just start off with John and now let’s add some more keys and values and to separate these keys and values we’re going to put in a comma so next let’s
Put in a key of age and a value of 25 and lastly we’ll put in a key of courses and for this value we’ll pass in a list of courses here so we’ll say math and compy so now let’s print out our student and see how that looks so if we save
That and run it then we can that it prints out all of our keys and values now let’s just get a value of one key so to do this we can add square brackets after our dictionary and then specify the key that we want to access so I can
Put in square brackets here and let’s say that we want to access the name of that student dictionary so I’ll just pass in the name key and run that and we can see that that gave us that value of that name key now if I wanted the
Courses instead then I could just pass in uh that courses for the key if I run that then you can see that we got this list of math and compi so we can see that these values in our dictionary can be just about anything our name is a
String our age is an integer and the courses are a list now all of our keys are currently strings but they can actually be any immutable data type so usually these will either be strings or integers but there are a few more data types that they can be as well so for
Example instead of name as our key here if for some reason we wanted this to be an integer so I’ll just pass in one so a one is a valid key and now if I access that key of one and run that then you can see that that gave us John but uh
I’m going to set that back to be a string for now and keep that as name so what happens if we try to access a key that doesn’t exist so for example I’ll try to access the key of phone for a phone number so if we run this then we
Can see that we get a key error because that phone key doesn’t exist now throwing an error when a key doesn’t exist might not always be what we want so sometimes we might just want to return none or a default value so to do this we can use the dictionary’s get
Method so instead of accessing this key this way if we were instead to say doget and use the get method um so let’s just go ahead and get the name since we know that that key already exists so if I run that then we can see that that works
Just like before and gave us the value of J on but if I try to access a key that doesn’t exist so we’ll try to access that phone key again if I save that and run it then by default this git method returns none instead of an error
And we can also specify a default value for keys that don’t exist so to do this we can just pass the default value that we want as a second argument to this git method so I’ll just put in a comma here and we’ll put in a string that just says
Not found so if I save that and run it now we can see that for key that don’t exist it returns not found um okay so let’s look at how we can add a new entry to our dictionary so let’s say that we wanted to add that phone number to our
Student dictionary and we’ll set this just above our print statement here so to do this it’s just as easy as saying student and then we will set the key that we want to set and set this equal to and we’ll just set this equal to a string of 555
5555 and if I save that and run it then we can see that it found that value of the phone key when we ran our print statement now if a key already exists if we set its value like this then it will update the value of that key so for
Example if right below this I was to say student and name is equal to and we’ll just pass in Jane if I save that and then print out I’ll comment out that for now if I print out our entire student variable then we can see down here that
The value for name was up updated when we assigned it to Jane now we can also update values using the update method now this is especially useful when we want to update multiple values at a time so for example let’s say that we wanted to add this phone number uh update this
Name and also update the age as well so to do this all in one shot we could say student. update and this takes in a dictionary as an argument and the dictionary is just everything that we either want to add or update so we can
Say that we want to update that name to Jane and we will update the age to let’s say 26 and we also want to add this phone key and that phone key will just set as what we had before 5555555 now if I save that and run it
Then we can see that just by running this one statement we updated the name to Jane the age is now 26 and it has this new key of phone number okay so now let’s say that we wanted to delete delete a specific key and its value now
One way that we can do this is by using the Dell keyword which stands for delete so let me just remove these updates here and then we can say so let’s say that we wanted to delete the students’s age so it’s as easy as just saying Dell student
Age and now if we run this then we can see that now the only keys that exist are name and courses so that age key was deleted now another way that we can remove a key and value is with the pop method so if remember from our video on
List the pop method will remove but also return that value so that allows us to grab the removed value with a variable so we could say um age is equal to and do a student. poop and what we want to pop is that age so if I save that then
We’ll also print the age here below student if I run that then we can see that the age and value were removed removed from our dictionary but we also created that age variable that contained the value that we popped off so that popped off 25 okay now let’s look at how
We can Loop through all the keys and values of our dictionary so first if we want to see how many keys we have in our dictionary then we can just print out its length with the Len function so if I was to say print Len of student and run
That then we can see that it returns three because we have three keys in our student iary now if we wanted to see all of these Keys then we could just print out student. keys if I run that then we can see that that gave us all of the
Keys of our dictionary if we wanted all of our values then we could print out student. values if I run that you can see that that gives us only the values now if we wanted to see the keys and values then we could use this items
Method if I run this then we can see that now we have these pairs of key and value pairs so we have name John age 25 courses with the list and we’ll be coming back to these pairs in just one second so if we wanted to Loop through
All of the keys and values in our dictionary we might be tempted to Loop through the same way we Loop through our list but if we just Loop through our list without using any method then it’ll just Loop through the keys so for example if you were to say four key in
Student and then print out that key if we run this then we can see that it just looped through and printed out all of those keys now in order to Loop through the keys and values we’ll need to use that items method that we just saw a
Second ago and so we’ll just plug that in there we’ll say student. items and now these come in a pair of two values so instead of just key we’re also going to have to access the value so we can say for key value and student. items and
Now we’ll print out the key and that value so if I run that so we can see that each Loop through this key variable was equal to each key and this value variable was equal to each value okay so I think that’s going to do it for this
Video I hope that now everyone feels comfortable working with dictionaries and the functionality that’s available to us and in the next video we’ll be going over conditionals and how to write if else and L if statements we’ll also be learning more about booleans and bullan operators now if anyone has any
Questions about what we covered in this video then feel free to ask in the comment section below and I’ll do my best to answer those and if you enjoy these tutorials and would like to support them then there are several ways you can do that the easiest way is to
Simply like the video and give it a thumbs up and also it’s a huge help to share these videos with anyone who you think would find them useful and if you have the means you can contribute through patreon and there’s a link to that page in the description section
Below be sure to subscribe for future videos and thank you all for watching
-
Sale!
Wireless WIFI Repeater Extender Amplifier Booster 300Mbps
$29.99$14.99 Add to cartWireless WIFI Repeater Extender Amplifier Booster 300Mbps
Categories: Electronics, Wi-Fi Router, Wireless Wi-Fi Extender Tags: 300Mbps, 802.11N, Amplifier, Booster, Extender, mobile wi-fi booster, Remote, WIFI, Wireless, Wireless WIFI, Wireless WIFI Repeater, Wireless WIFI Repeater Extender, Wireless WIFI Repeater Extender Amplifier, Wireless WIFI Repeater Extender Amplifier Booster, Wireless WIFI Repeater Extender Amplifier Booster 300Mbps$29.99$14.99 -
Sale!
Full RGB Light Design Gaming Headset Headphones with Mic
$24.99$14.99 Add to cartFull RGB Light Design Gaming Headset Headphones with Mic
Categories: Electronics, Gaming, Gaming Headsets Tags: Design, Full, Full RGB Light Design Gaming Headset, Full RGB Light Design Gaming Headset Headphones, Full RGB Light Design Gaming Headset Headphones with Mic, Gamer, Gaming, Gaming Headset Headphones, gaming headset wireless, Headphone, Headphones, Headset, Light, Mic, Package, RGB$24.99$14.99 -
Sale!
Wireless BlueTooth Multi-Device Keyboard Mouse Combo
$39.99$19.99 Add to cartWireless BlueTooth Multi-Device Keyboard Mouse Combo
Categories: Electronics, Gaming, Gaming Keyboards, Keyboard Mouse Combos Tags: Combo, Keyboard, keyboard mouse combos, Mouse, MultiDevice, Set, WireKeyboard Mouse Combo, Wireless, Wireless BlueTooth Keyboard Mouse Combo, Wireless BlueTooth Keyboard Mouse Combos, Wireless BlueTooth Multi-Device Keyboard Mouse Combo, Wireless BlueTooth Multi-Device Keyboard Mouse Combos$39.99$19.99 -
Sale!
High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
$199.99$139.99 Add to cartHigh Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
Categories: Gaming, Gaming Chairs Tags: Adjustable, Chair, computer chairs, Desk, Executive, Gaming, Girl, Headrest, High, High Back Leather Executive Adjustable Swivel Gaming Chair, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar, High Back Leather Executive Adjustable Swivel Gaming Chairs, Leather, Lumbar, Office, Racing, Swivel$199.99$139.99 -
Sale!
Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
$29.99$19.99 Select optionsProfessional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
SKU: N/A Categories: Electronics, Gaming, Gaming Headsets Tags: Cancelling, Gaming, Gaming Headphones with Noise Cancelling Microphone, gaming headset, Headphones, Headset, LED, Light, Mic, Microphone, Noise, Professional, Professional LED Light Wired Gaming Headphones, Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone, Wired, Wired Gaming Headphones, Wired Gaming Headphones with Noise Cancelling Microphone$29.99$19.99 -
Sale!
Gaming Desk with LED Lights USB Power Outlets and Charging Ports
$349.99$249.99 Select optionsGaming Desk with LED Lights USB Power Outlets and Charging Ports
SKU: N/A Categories: Computer Desk, Gaming, Gaming Desk Tags: and Charging Ports, Charging, Desk, Desks, Gaming, gaming desk with led lights, Gaming Desks with LED Lights, Home, LED, Lights, Monitor, Office, Outlets, Port, Power, Room, Stand, USB, USB Power Outlets, White, Workstation$349.99$249.99 -
Sale!
Wired Mixed Backlit Anti-Ghosting Gaming Keyboard
$99.99$79.99 Add to cartWired Mixed Backlit Anti-Ghosting Gaming Keyboard
Categories: Electronics, Gaming, Gaming Keyboards Tags: Antighosting, Backlit, Blue, brown, Gaming, Gaming Keyboard, gaming keyboards, gaming keyboards and mouse, Keyboard, Laptop, Switch, Wired, Wired Mixed Backlit Anti-Ghosting Gaming Keyboard, Wired Mixed Backlit Anti-Ghosting Gaming Keyboards, Wired Mixed Backlit Gaming Keyboard$99.99$79.99 -
Sale!
Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
$119.99$59.99 Add to cartWireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
Categories: Electronics, Gaming, Gaming Headsets Tags: 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, ANC, Audio, Bluetooth, Cancellation, Ear, Earphone, gaming headset, Headphones, Headset, Hi-Res Over the Ear Headphones Headset, HiRes, Noise, Wireless, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Headphones, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headsets$119.99$59.99 -
Sale!
Wired Sports Gaming Headset Earbuds with Microphone
$19.99$9.99 Select optionsWired Sports Gaming Headset Earbuds with Microphone
SKU: N/A Categories: Gaming, Gaming Headsets Tags: Accessories, Earbud, Earphone, Earphones, Gaming, gaming headset with microphone, Headphones, Headset, IOS, Microphone, Sports, Wired, Wired Sports Gaming Headset Earbuds, Wired Sports Gaming Headset Earbuds with Microphone, Wired Sports Headset Earbuds$19.99$9.99 -
Sale!
150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
$49.99$29.99 Add to cart150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
Categories: Charging Stations, Electronics Tags: 150W, 150W Charging Station, 150W Universal Multi USB Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Stations, 150W Universal Multi USB MAX Charging Station, 16 Port MAX Charging Station, 3.5A, Charger, Charging, Fast, laptop charging stations, Max, Multi, Port, Stand, Station, Universal, USB$49.99$29.99
Definite like and subscribe here. Extremely well thought out and direct assistance through example. Thanks man you saved me a million migraines
Independent government
GOAT!
You speak fast but you are good
Thank you
Thank you for making this east
Great explanation!
Hi Corey I was just wondering is it crucial to learn Data Structures and Algorithms and know BIG O notation in a data analytics position?
thank you sir
Thanks you Corey!
Brilliant tutorials!
… but how to build the Python dictionary from a MongoDB collection ?
Short and straight to the point. Nice vid m8
Very helpful thank you
Mr. Schafer, I've never felt so close to learning these concepts after spending so many Python courses… I will be donating for sure.
Thanks!
This is truly one of best tutorials out there…..Subscribe!!!!!!!!!😊😊
Thank you so much for your videos they are simply amazing! You have great skills for teaching and explaining topics in a manner that makes them easy to understand, very clear and optimized in the simplests ways.
I have a question involving dictionaries, what makes them different from creating variables for the same values as you would in a dictionary?
You could just create a name variable for a student's name and an age variable for a student's age, why bother with the dictionary?
Can you do tutorials on playing web games bot
Ok, but how store multiple students? In your case name key will always change the name of only one user
This video was very useful, thank you!
awesome, I see example problem in my book with answer and cannot figure it out but your video explains. Helped me at 8:32
🙅
👫
Awesome video ! U r a natural teacher ! Loved it
Cool video.
thank you kind sir
I've been learning Python for a while, and you are gifted. Many thanks
Great Sir. I larned more than my University classes.
Hi Corey, I have a question, what combination of keys did you enter to highlight your code and remove the # on the left of your code.
Effortless and excellent teaching by a born teacher.
how to make it there's : in each of the key
dude…this just explained so much to me in such little time. I have been scratching my head for a while over things and not really understanding some looping within these dictionaries, but your explanation makes so much more sense that its actually starting to come together. Thank you.
ummmm.. this guy is excellent, thank you
How do you come to know when to use (), {} or []. I see them being used here a lot, I am a new learner so getting confused in this
Hey Corey, I know these videos are geared towards beginners like myself – and also a few years old at this point – but it would be helpful to explain how each concept in the tutorials are used in the real world, beyond just printing out values.
Hopefully this makes sense! Thank you for the instruction – I appreciate your teaching style
👬
If Corey> Mosh:
print("Corey is the Best")
It would be an extension to talks about a dictionary of student of students
Sir . dictionary can't support "update" now. So give a sollution plz.
Why do all programmers talk so fast?
Здорово!