Dictionary In Python | Python Dictionary Tutorial | Python Certification Training | Edureka

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello guys this has were seen from a Eureka and I welcome you all to this session where I'll be talking about dictionary in Python as you guys all know Python programming language is one of the most popular programming language nowadays it provides us with numerous applications and various out-of-the-box features one such concept is the dictionary in Python in this session we will learn about Python dictionary in detail before moving on let's take a look at the agenda for this session first of all I'll be starting with what exactly is a dictionary in Python and why do we use a dictionary then we will compare the dictionary with the lists moving further we will learn how to implement a dictionary and various operations that we have for dictionary in Python and finally I will sum up this session with a small use case to explain the concept of nested dictionary in pipe I hope you guys are clear with the agenda also I suggest you to subscribe to the Eddie recur channel to kick-start your learning and also press the bell icon to get the latest updates from Eddie Waker now without any further ado let's try to understand what is a dictionary in Python so what is the dictionary in Python it is a collection datatype just like a set or a list but there are certain features that makes dictionary unique so let's take a look at the features of Python dictionary guys first of all it is unordered and we can change values in a dictionary as well since it is mutable also it has key value pairs which is like a map that we have in other programming languages and it is indexed as well since the keys are distinct and can be used as indexes to access or change any values in the dictionary a dictionary does not have any duplicate members as well now that we know what dictionary in Python is let's talk about why we use a dictionary in Python first of all the reason would be it is unordered and stores data like a map so it is one unique feature that Python dictionary has and it also contains the key value pairs unlike any other data type or object in Python it is almost similar to a real-life dictionary where we have distinct values and corresponding to these values there are respective definition in case of the dictionary even though there are no duplicate members we can mention duplicate members in the values but the keys has to be unique in order to access these members since there are no indexes also we use the keys as indexes when accessing these elements now that we have learned about why views are dictionary let's understand how it is different from a list in bathin so I've listed down these differences between a list an additional ease so first of all list is ordered but the dictionary is not dictionary is actually unordered guys the next one is list has indexes but in order to access elements in a dictionary use the keys as indexes the next one would be the list is collection of elements but in dictionary we have the key value pairs as elements which is unlike any other data type or object in Python the next one is list is often preferred for ordered data but dictionary is preferred when the data has unique key values so this actually differentiates a list from our dictionary guys and the next one is list have duplicate elements as well but in dictionary we do not have any duplicate elements when we are declaring the keys although we can have duplicate members when we are specifying the values for each key so these are the differences between a dictionary and a list guys I hope you are clear with the difference between those two now that we have understood what our dictionary is in Python and why we use it and how it is different from a list let's try to understand how we implement a dictionary in pipe so first of all to declare a dictionary in Python we have to use the curly brackets just like you can see in the example here I have the dictionary with the name my dictionary and inside those I have the key value pairs inside the curly brackets so let's take it up to pycharm nice to understand how we implement a dictionary in Python let's enter the presentation mode guys so we will try to implement a dictionary here let's take the name of the dictionary as a so inside the curly brackets I can give the key value pairs so I'll give one as my first key so when I am specifying the key value pairs I have to use the colon to separate the key from the value now I'll give one value over here now when we are declaring a dictionary we have to separate these key value pairs with a comma I'll give one other key give it a value let's say data science I'll give one more key and give it a value let's say Python so this is my dictionary guys this is how you actually implement a dictionary I have declared a dictionary over here now let's try to understand how I'm going to access these elements so I'll just write a inside the square brackets I'll specify has a key value so I'll put it inside a print statement guys now as you can see when I run this I will get the value that is specified in the first key value which is Eddie rekha I can simply just print all of my dictionary guys so this is how I'm going to get the output as when I print the dictionary that I've just specified now to access these elements I can also use the get function and inside the cat function I'll just specify the key value and when I run this I will get the same output and as I was getting using the square brackets guys so this is how you access elements inside the dictionary now I can also replace values inside a dictionary guide since it is mutable so instead of data science let's say I want machine learning so what I'll do is I'll just specify the key value and I will specify the value that I want to replace it with now when I print this you will see the replaced value inside the dictionary as you can see here I have replaced the value inside the dictionary guys now if I want to add one more value inside a dictionary what I'm going to do is I'll just specify the key value that is let's say 4 because we don't have 4 over here so instead of machine learning I'll add the value data signs that I have just replaced now when I print my dictionary here let's see what all values I will get I will get one more value that I have just added which is data science so this is how you actually implement a dictionary guys and to access these values you can either use the square brackets and inside those square brackets you can specify the key value or you can just use the get function and inside the get function and inside the gate function you have to specify the key value as well so now that we have understood how we can implement a dictionary in Python let's try to take a look at the operations that we have in Python dictionary guys so as I have told already told you how you can actually add an element to your dictionary just like this I have specified a key value and given it a value and then there is a replacing element which I have already shown you guys so these are all the operations that we have in Python dictionaries so we will look at them one by one nice so let's take it up to pycharm and understand these operations guys so we'll once again enter the presentation mode so we have a dictionary over here guys so first of all let's talk about clear so when I use the clear what it's going to do is it is going to remove all the elements from the dictionary so when I run this so I have a dictionary guys that I've already specified so let's try to print the dictionary first so you will understand what all these operations are going to do so I have this dictionary which has these values all right so I'll do one thing I'll use the first operation that is clear let's see what it does to our dictionary guys when I run this okay so now when I print my dictionary let's see what our values are get it is saying that it is empty because the clear function has actually deleted or removed all the elements from the dictionary so this is what clear does guys now let's take a look at the another one which we have also I would want to declare this dictionary once again and when I print this I should be getting all these values again yes I am getting these values so it would be fine if I do one more thing so I'll just use the copy over here so what copy does is it returns a copy of the dictionary guys so let's say when I print B over here what is going to happen it is going to show me the copy of this dictionary that I've just specified over here using the copy function so let's take a look at the next one we have which is the values so when we are using the values function it what it's going to do is it is going to return all the values inside the dictionary guys so when I run this it will print all the dictionary values inside a list which is as you take our data signs and python so we look at the next one we have which is the date function so what does update function do update function actually updates the values of the dictionary with the specified key value pairs so let's say if I have use update just like this what will happen we have no updates which is it is showing that we haven't updated anything so this is what update does guys if we have made any updates it will update the changes into the dictionary so we look at the next function that we have which is actually get function I've shown you how to use the get function we will just use the get function to get the values and inside this I just specify the key value and it will return the value with the specified key which is actually a TI Rekha so this is how you actually access the elements using the get function next one is the items function guys so I'll just use items over here and inside this I will get the list for a couple of each key value pair wise so let's run this so when I run this I am getting couple of each key value pair as you can see over here I have two then I have the value and I have one more key so this is a couple of each key value pair which we can get using the items function now coming back to the next one that we have which is the keys function so what it will do is it will actually return all the keys inside the dictionary that we have which is one two three guys so this is all about keys function guys so let's talk about the pop function guys so when I use the pop function what it will do is it will actually pop the value with the specified key so I'll just pop this from the dictionary with the value three so as you can see I am getting that value over here guys now let's take a look at the next function that we have or the operation which is pop items guys so when I'm using the pop item function I can actually remove the last inserted key value pill so I don't have to specify any values over here as you can see I'm getting the couple of the last key value pair I have specified inside the dictionary so this is what pop item does on the next function that we have is set default guys so what set default does is it returns the value of the specified key if the key does not exist we can insert the key with the specified value so I'll specify the value over here let's say one now when I print this I'm getting le Rekha as well so when I specify the next key it is showing me okay it's a syntax error I'm getting the next value so this is what set default is used for which it returns the value of the specified key guys so this is all about the operations that we have in a dictionary guys there is one more thing to discuss about the dictionary guys which is the dictionary constructor so the dickster any constructor is actually used to declare a dictionary guys so let's take it up to pycharm again so I'll show you what a dictionary constructor does so I'll use a I'll use okay I'll use mine dictionary as my variable dictionary name and I'll use the dictionary constructor over here I'll specify the values so what I'm going to do is I'll just specify the values I will use the keys as let's say key 1 only and then I'll give it a value let's say and you rekha now I can give another key value let's say 2 and I'll give it a value let's say Python now let's see if I can get a dictionary from this so using the dictionary constructor let's see if we have actually implemented a dictionary over here so as you can see I have declared a dictionary using the dictionary constructor guys so when I run this I should be getting a dictionary here so as you can see I have declared a dictionary using the dictionary constructor also one more thing I have missed over here when I was explaining the operations guys so which is the from keys operation so I will declare one more dictionary over here give it a value let's say 1 2 I'm giving you the random values guys don't be alarmed now when I'm using the frumpkis keyword what I do is I'll just say a dot from okay we don't have a over here so I'll write a from keys I'll specify my dictionary over here so let's see what happens guys okay so I will just use the print statement and inside this I will use the from keys and inside this I'll specify my dictionary let's see what happens when I run this so I am getting a dictionary with the specified keys and values so as we don't have any key one key to inside this so you know we are getting the non value over here so this is what from keys thus inside our dictionary guys so now that we have actually learned what is the dictionary and how we can implement it in Python and all sorts of operations that we can perform on a dictionary let's try to understand what are nested dictionary guys so for this I have a problem statement guys so now that we know that ICC Cricket World Cup is just around the corner guys so what we will do is we will implement a dictionary with the stats of the 15 members from the Indian cricket squad for the World Cup 2019 and we will use the concept of nested dictionary in the use case so what we will do is will implement a dictionary first and then we will make the unique keys to access those values when we are actually getting the elements from the dictionary and then we will store the data inside the dictionary and we will also import a panda's package and convert those data into a data frame for better display statistics so let's take it up to PyCharm guys so ok let's exit the presentation mode so over here I have the value or the dictionary that I have implemented so let's check it up too and presentation mode again so as you can see I have this module that I have actually imported from pandas and I have given it an alias as PD so we'll talk about this later so this is my dictionary guys in which I have made four more dictionaries guys which is actually are nested dictionaries so first key is batsmen inside the batsman key I have all those batsmen like Rohit Sharma secret oven and Virat Kohli and inside those values I have one more dictionary in which we have the statistics for all those matches runs average high score so actually inside this batsman key I have two more dictionary guys so this is the concept of nested dictionary so similarly I have made it for all-rounder and we have wicked keepers and bowlers as well so this is my dictionary guys that I have actually stored all the data using the dictionary or as you can say nested dictionary and then I have all these statements that I have written in which I have converted the data inside the dictionary into a data frame wise so I have this print statement so first of all let me print the batsman squad in which we will get the values for all the batsmen that we have inside the World Cup squad so as you can see I'm getting a data frame over here so I have these values we have which are Michiko Dhawan and without coulis so as you can see we have average highest score matches and runs over here so for Virat Kohli we have more than 10,000 runs Shikata one has 5000 runs and Roy Sherma has around 8,000 runs guys so let's try to understand more from the dictionary that I have just mentioned over here we have got all the batsmen now we will try to get all the bowlers so I will just print this statement and it will show me all the bowler statistics which is cool DiPiazza Mohammed Shah me boom Ashok Kumar used way in the child so we have all these statistics in which we have average we have best figures then we have matches and wickets as well so as you can see we have bhuvaneswar kumar with the most wickets which is 118 wicked skies so this is what you can actually do using the nested dictionaries guys as you can see this is an unordered data but we have key value pairs which actually helps in retrieving the data very easily guys so let's say inside bowler I won't just feed boom bra or for all-rounder I want hardik Pandya all these statements I can use it for getting all those values inside a data frame now that we have come to the end of the session I hope you guys are clear with the concept of dictionaries in Python eyes if you have any questions you can put it in the comment section and we will get back to you as soon as possible thank you I hope you have enjoyed listening to this video please be kind enough to like it and you can comment any of your doubts and queries and we will reply them at the earliest do look out for more videos in our playlist and subscribe to any Rekha channel to learn more happy learning
Info
Channel: edureka!
Views: 63,549
Rating: 4.6669497 out of 5
Keywords: yt:cc=on, dictionary in python, python dictionary, python dictonary tutorial, dictionaries in python, python dict, python dictionary loop, python dictionary comprehension, python dictionary basics, python dictionary key value, python dictionary examples, python dictionary explained, python dictionaries, dictionary in python 3, key value pair, python lists and dictionaries, using dictionaries in python, python 3 dictionary, python edureka, edureka
Id: rZjhId0VkuY
Channel Id: undefined
Length: 17min 28sec (1048 seconds)
Published: Thu May 23 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.