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




PYTHON - INSERT IN DICTIONARY


How to insert a new Item in a Dictionary ?


Let us say, we have a Dictionary that contains,



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


As 'Key' and 'Value' pair.


Now, let us say, we want to insert a new entry,


Rose, Is a Flower


Where 'Rose' is the 'Key' and 'Is a Flower' is its corresponding 'Value'.


This can be achieved by two ways :


  1. Assigning the 'Value' directly by using the 'Key'.
  2. Using the 'update( )' Function.

Let us see in the below example using the first way.


Example :


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


Output :



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

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

Now, we are supposed to add a new entry to the 'Dictionary'. Where the 'Key' would be 'Rose' and the Value 'Is a Flower'.


So, we have used the below way to add it.


x["Rose"] = "Is a Flower"

java_Collections

And the entry is added to the 'Dictionary'.


java_Collections

And we get the below output,


Output :



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

Let us see the second way of adding a 'Key' 'Value' using the 'update( )' Function.


Example :


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


Output :



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

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

Now, we are supposed to add a new entry to the 'Dictionary'. Where the 'Key' would be 'Rose' and the Value 'Is a Flower'.


So, we have used the 'update( )' Function to add it.


x.update({"Rose": "Is a Flower"})

And the entry is added to the 'Dictionary'.


java_Collections

And we get the below output,


Output :



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

Duplicate Keys are not allowed in Dictionary


Let us say, we have a Dictionary that contains,



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


As 'Key' and 'Value' pair.


Now, let us say, we want to insert a new entry,


John, Is an English Name


Where 'John' is the 'Key' and 'Is an English Name' is its corresponding 'Value'.


Let us see in the below example.


Example :


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


Output :



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

So, for the 'Key', 'John' the initial value was 'Is a Name'.


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

And we wanted to add a new entry with the 'Key' as 'John' and the 'Value' as 'Is an English Name'.


x["John"] = "Is an English Name"

But if we see the output.


Output :



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

We can see the 'Key', 'John' is updated with the 'Value', 'Is an English Name'.


That's because duplicate 'Keys' are not allowed in Dictionary. The corresponding value for that 'Key' will get updated with the new 'Value'.


Note : Just remember, only duplicate 'Keys' are not allowed in a 'Dictionary'. However, you can have duplicate 'Values'.