Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




PYTHON - DICTIONARY


A 'Dictionary' is a Collection that can also hold multiple values in 'Key' and 'Value' pairs.In the 'Dictionary', the elements are unordered, unindexed, changeable and doesn't allow duplicate values.


You can think of a 'Dictionary' like an actual English Dictionaries. Where you search for a word and you get its explanation.


The declaration of a 'Dictionary' is also quite simple. You can place the multiple values inside braces '{ }' in the form of 'Key' and 'Value' pairs.


Just imagine the 'Key' to be the word you are going to search in the English Dictionary.And the 'Value' is the explanation you find in it.


Creating a 'Dictionary' with values of different Data Types


Example :


x = {
    5 : "Is a Number", 
    "John": "Is a Name", 
    "Python": "Is a Language"
}
print(x)


Output :



  {5: 'Is a Number', 'John': 'Is a Name', 'Python': 'Is a Language'}

So, in the above code we have created a 'Dictionary' using square braces '{ }'. And the values are represented in 'Key' and 'Value' pairs.


The 'Key' is a number or a String and in the value we have provided its explanation(Somewhat like the English Dictionary).


i.e. We know that the first element '5' is a number. So, in the 'Key', we have put the number '5' and in its value, we have put the explanation, 'Is a Number'. Same logic is applicable for the other two 'Keys', 'John' and 'Python'.


A 'Key' and 'Value' is separated by ':'.


x = {
    5 : "Is a Number", 
    "John": "Is a Name", 
    "Python": "Is a Language"
}

And initialised to the variable 'x'.


java_Collections

And in the next line we have printed the 'Dictionary' using the print statement.


print(x)

Now, if we see the output,


Output :



  {5: 'Is a Number', 'John': 'Is a Name', 'Python': 'Is a Language'}

Iterating a Dictionary using 'for loop' and getting the 'Keys'


Example :


x = {
    5 : "Is a Number", 
    "John": "Is a Name", 
    "Python": "Is a Language"
}
for i in x:
    print(i)


Output :



  5
  John
  Python

Similarly, in the above code we have created a 'Dictionary' using braces '{ }' and 'Key' and 'Value' pairs.


x = {
    5 : "Is a Number", 
    "John": "Is a Name", 
    "Python": "Is a Language"
}

And initialised to the variable 'x'.


java_Collections

In the next line we have used the 'for loop' to Iterate through the 'Dictionary'.


for i in x:
    print(i)

As we know, there is a 'Key' and 'Value' pair in a 'Dictionary'. But we are just getting the 'Keys'


Let us understand it in the iterations of 'for' loop,


for i in x:

1st Iteration


In the first Iteration the first 'Key' of the 'Dictionary' 'x' (i.e. The key '5') is taken and put into the variables 'i'.


java_Collections

And the print statement, prints the value of 'i'(i.e. The key).


Output :



  5

2nd Iteration


Similarly, in the second Iteration the second 'Key' of the 'Dictionary' 'x' (i.e. The key 'John') is taken and put into the variables 'i'.


java_Collections

And the print statement, prints the value of 'i'(i.e. The key).


Output :



  5
  John

3rd Iteration


Similarly, in the third Iteration the third 'Key' of the 'Dictionary' 'x' (i.e. The key 'Python') is taken and put into the variables 'i'.


java_Collections

And the print statement, prints the value of 'i'(i.e. The key).


Output :



  5 Is a Number
  John Is a Name
  Python Is a Language

So, we have seen how to get the 'Keys'. Now, let us see how can we get the corresponding 'Value' associated with that 'Key'.


It is just like searching a word in an English dictionary and get its corresponding value.


Iterating a Dictionary using 'for loop' and getting the 'Value' for a 'Key'


Example :


x = {
    5 : "Is a Number", 
    "John": "Is a Name", 
    "Python": "Is a Language"
}
for i in x:
    print("The value for the key ",i," is ",x[i])


Output :



  The value for the key 5 is Is a Number
  The value for the key John is Is a Name
  The value for the key Python is Is a Language

Similarly, in the above code we have created a 'Dictionary' using braces '{ }' and 'Key' and 'Value' pairs.


x = {
    5 : "Is a Number", 
    "John": "Is a Name", 
    "Python": "Is a Language"
}

And initialised to the variable 'x'.


java_Collections

In the next line we have used the 'for loop' to Iterate through the 'Dictionary'.


for i in x:
    print("The value for the key ",i," is ",x[i])

As we know, there is a 'Key' and 'Value' pair in a 'Dictionary'. And in each Iteration, we are fetching the 'Value' for a particular 'Key'.


Let us understand it in the iterations of 'for' loop,


for i in x:

1st Iteration


In the first Iteration the first 'Key' of the 'Dictionary' 'x' (i.e. The key '5') is taken and put into the variables 'i'.


java_Collections

Then in the print statement, we have used the 'Key' (i.e. '5') to get the value associated to it 'x[i]')


java_Collections

Where,


java_Collections

And the print statement,


print("The value for the key ",i," is ",x[i])

Prints the 'Key' and 'Value'.


Output :



  The value for the key 5 is Is a Number

2nd Iteration


Similarly, in the second Iteration the second 'Key' of the 'Dictionary' 'x' (i.e. The key 'John') is taken and put into the variables 'i'.


java_Collections

Then in the print statement, we have used the 'Key' (i.e. 'John') to get the value associated to it 'x[i]')


java_Collections

And the print statement,


print("The value for the key ",i," is ",x[i])

Prints the 'Key' and 'Value'.


Output :



  The value for the key 5 is Is a Number
  The value for the key John is Is a Name

3rd Iteration


Similarly, in the third Iteration the third 'Key' of the 'Dictionary' 'x' (i.e. The key 'John') is taken and put into the variables 'i'.


java_Collections

Then in the print statement, we have used the 'Key' (i.e. 'Python') to get the value associated to it 'x[i]')


java_Collections

And the print statement,


print("The value for the key ",i," is ",x[i])

Prints the 'Key' and 'Value'.


Output :



  The value for the key 5 is Is a Number
  The value for the key John is Is a Name
  The value for the key Python is Is a Language

Iterating a Dictionary using 'for loop' and items( ) Function


Example :


x = {
    5 : "Is a Number", 
    "John": "Is a Name", 
    "Python": "Is a Language"
}
for i,j in x.items():
    print(i, j)    


Output :



  5 Is a Number
  John Is a Name
  Python Is a Language

Similarly, in the above code we have created a 'Dictionary' using braces '{ }' and 'Key' and 'Value' pairs.


x = {
    5 : "Is a Number", 
    "John": "Is a Name", 
    "Python": "Is a Language"
}

And initialised to the variable 'x'.


java_Collections

In the next line we have used the 'for loop' to Iterate through the 'Dictionary'.


for i,j in x.items():
    print(i, j)

Now, since there is a 'Key' and 'Value' pair in a 'Dictionary', two variables 'i' and 'j' are used. 'i' to hold the key and 'j' to hold the value.


And to get all the elements of the 'Dictionary', the 'items( )' Function is used.


Now, if we see the iterations of 'for' loop,


for i,j in x.items( ):

1st Iteration


In the first Iteration the first value of the 'Dictionary' 'x' (i.e. The key '5' and the value 'Is a Number') is taken and put into the variables 'i' and 'j'.


java_Collections

And the print statement, prints the value of 'i'(i.e. The key) and 'j'(i.e. The value).


Output :



  5 Is a Number

2nd Iteration


Similarly, in the second Iteration the second value of the 'Dictionary' 'x' (i.e. The key 'John' and the value 'Is a Name') is taken and put into the variables 'i' and 'j'.


java_Collections

And the print statement, prints the value of 'i'(i.e. The key) and 'j'(i.e. The value).


Output :



  5 Is a Number
  John Is a Name

3rd Iteration


Similarly, in the third Iteration the third value of the 'Dictionary' 'x' (i.e. The key 'Python' and the value 'Is a Language') is taken and put into the variables 'i' and 'j'.


java_Collections

And the print statement, prints the value of 'i'(i.e. The key) and 'j'(i.e. The value).


Output :



  5 Is a Number
  John Is a Name
  Python Is a Language

So far, we have seen, how to access all the elements of a 'Dictionary'. i.e. Both the 'Key' and 'Value'.


Now, what if we want to access just the 'Key' or the 'Value'. Let us see in the next example.


Iterating a Dictionary using 'for loop' to print the Keys only


Example :


x = {
    5 : "Is a Number", 
    "John": "Is a Name", 
    "Python": "Is a Language"
}
for i in x.keys():
    print(i)


Output :



  5
  John
  Python

So, in the above code we have created a 'Dictionary' using braces '{ }' and 'Key' and 'Value' pairs.


x = {
    5 : "Is a Number", 
    "John": "Is a Name", 
    "Python": "Is a Language"
}

And initialised to the variable 'x'.


java_Collections

In the next line we have used the 'for loop' to Iterate through the 'Dictionary' just to get the 'Keys'.


for i in x.keys():
    print(i)

Now, since there is a 'Key' and 'Value' pair in a 'Dictionary', we have used the 'keys( )' Function, that will only print the 'Keys' and not the 'Values'.


And if you see the Output, just the 'Keys' are printed.


Output :



  5
  John
  Python

Next, let us see, how can we access only 'Values'.


Iterating a Dictionary using 'for loop' to print the Values only


Example :


x = {
    5 : "Is a Number", 
    "John": "Is a Name", 
    "Python": "Is a Language"
}
for i in x.values():
    print(i)


Output :



  Is a Number
  Is a Name
  Is a Language

So, in the above code we have created a 'Dictionary' using braces '{ }' and 'Key' and 'Value' pairs.


x = {
    5 : "Is a Number", 
    "John": "Is a Name", 
    "Python": "Is a Language"
}

And initialised to the variable 'x'.


java_Collections

In the next line we have used the 'for loop' to Iterate through the 'Dictionary' just to get the 'Values'.


for i in x.values():
    print(i)

Now, since there is a 'Key' and 'Value' pair in a 'Dictionary', we have used the 'values( )' Function, that will only print the 'Values' and not the 'Keys'.


And if you see the Output, just the 'Values' are printed.


Output :



  Is a Number
  Is a Name
  Is a Language

Next, let us see, how to access the elements of the Dictionary.