Iterating through the items of a Dictionary

Iterating through the items of a Dictionary

Lesson Details:
September 17, 2020

Video Transcription: In this lecture I am going to show you how to eye trait through the elements of a dictionary using for loop this video is brought to you by Appy Pie’s Academy, so let me first give the comment height rating through white rating through the elements of our dictionary using for loop of a dictionary is using for loop okay it's quite easy to do it because we have already used for loop for titrating through the items of two pulse and set etcetera and lists also so in order to define a dictionary there is another method you can use this function dict this method dict so it is almost akin to the method that we have been using earlier so Cities is equal to dict and within parentheses giving the pairs of my items of my elements for example a colon amritsar so key is a value is Omri sir then one column Ludhiana sorry l : Dodona l is for and is the key Ludhiana is the value C is the key Chandigarh is the value then I am going to apply the loop and for VAR in cities let me write cities the name of the dictionary and within parentheses sorry within square brackets I would write the variable bar now how does it work so when the loop will execute for the very first time the value of ver will be the key a so it will become cities a that means the value of value am reserve will be printed okay now when the loop will go to the second turn the value of where will be changed to the key L and corresponding value of L is Ludhiana so when I would write Cities LD Anna will be printed yes and third time over that is the last time the value of where will matically become C and what will happen is it will become cities C and what is the corresponding value of city C chandigarh Sookie is C and value is Chandigarh so cities C will print Chandigarh so so far so good let me execute the program there seems to be some indentation error I shouldn't have given the space okay so invalid syntax oh okay actually instead of Colin I have one comma between a and M reserve that was a minor mistake okay now we have executed the program by pressing ctrl shift f10 and see it has I trated through all the utter items of our dictionary Amritsar Ludhiana Chandigarh.

loader
Course content