How to create python dictionary?

Dictionary

Dictionaries are used to store data values in key:value pairs.

A dictionary is a collection which is unordered, changeable and does not allow duplicates.

Dictionaries are written with curly brackets, and have keys and values:

Best Python Training Institute in Noida

Example

Create and print a dictionary:

thisdict = {
  
"brand""Ford",
  
"model""Mustang",
  
"year"1964
}
print(thisdict)

Dictionary Items

Dictionary items are unordered, changeable, and does not allow duplicates.

Dictionary items are presented in key:value pairs, and can be referred to by using the key name.

Example

Print the "brand" value of the dictionary:

thisdict = {
  
"brand""Ford",
  
"model""Mustang",
  
"year"1964
}
print(thisdict["brand"])

 

Unordered

When we say that dictionaries are unordered, it means that the items does not have a defined order, you cannot refer to an item by using an index.

Changeable

Dictionaries are changeable, meaning that we can change, add or remove items after the dictionary has been created.


Duplicates Not Allowed

Dictionaries cannot have two items with the same key:

Example

Duplicate values will overwrite existing values:

thisdict = {
  
"brand""Ford",
  
"model""Mustang",
  
"year"1964,
  
"year"2020
}
print(thisdict)

Get More Information About Python Training and Certification Course Visit Here.


Thank You for Reading

Written by Jitender Kumar

Comments