Python Dictionary a simple Explanation

Python Dictionary

In this article, we are going to learn about another data structure Python Dictionary a simple Explanation with code example. We will learn about the basic characteristics of Python dictionaries like how to create, how to access its elements, how to manage data in it.

What is Dictionary?

Dictionary is a collection that is ordered, which makes it a composite data type in Python. Dictionaries are Python’s implementation of a data structure that is more generally known as an associative array. A dictionary consists of a collection of key-value pairs. Each key-value pair maps the key to its associated value. Dictionary is listed in curly brackets, inside these curly brackets, keys and values are declared. Each key is separated from its value by a colon (:), while commas separate each element.

So let us begin with the Properties of Dictionaries.

Properties of Python Dictionary:

  • Dictionaries are mutable, which means that we can edit, add or remove items after the dictionary has been created. Dictionaries are dynamic in nature and they can grow in size or shrink in size at run time as per our program need.
  • Dictionaries can be nested into each other, which means a dictionary can contain another dictionary.
  • Dictionary elements can be accessed by using keys.
  • Dictionaries hold the data as key-value pairs. For each key, we have a value that makes the dictionaries very easy to access.
  • Dictionary keys do not allow polymorphism means it allows only unique keys. Dictionaries do not allow two items with the same key. All key has to be unique.
  • Dictionary keys are case sensitive, which means you can use the same key with a different case and they will be treated as different keys. For example, name and NAME are two different keys.
  • Dictionaries are unordered and their data elements are not accessible in a defined order, this is a reason we can not use indexing to access dictionary elements. We need to use keys to access dictionary elements.
  • The data elements in the dictionary can be of any data type. You can have data of any basic or user-defined data types in dictionary-like strings, int, boolean, lists, etc.

How to create a dictionary in Python?

We can create dictionaries in many ways in Python. So we will learn different methods now to create dictionaries. Let us start with an empty dictionary.In Python two ways to create Dictionary in Python

  • Using {} bracket
  • Using dict() method

Let us understand both ways better using some code example

1.Create dictionary using {} brackets

Syntax for create Dictionary using {} brackets

Dict = { key:value,..........key:value }

Example 1: Create an empty Dictionary

An empty dictionary is used as a placeholder and it does not have any data in it. An empty dictionary can be created by just placing to curly braces{ }.

#an empty Dictionary 

first_dict = {}

print("my first empty dictionary: ")

print(first_dict )


#output :
my first empty dictionary: 
{}

Example 2: Create a Dictionary with int keys, string keys, nested dictionary

In this example we are creating with integer keys,string keys,nested keys using {} brackets.

#create a dictionary with data integer keys

first_dict = {1:'Dog', 2:'cat',3:'rat'}

print("dictionary with integer key:\n ")

print(first_dict)



#create a dictionary with string as key type

first_dict = {'name':'Dog', 'pet':'cat','animal':'rat'}

print("dictionary with string as key type:\n ")

print(first_dict)


#Create a nested dictionary

first_dict = {'name':'Dog', 'pet':False,'age':12,'Flower':{'fruits':'apple','kiwi':'berry',1:'A'}}


print("Create nested dictionary :\n ")

print(first_dict)


Output

dictionary with data integer key :
 
{1: 'Dog', 2: 'cat', 3: 'rat'}


dictionary with string as key type:
 
{'name': 'Dog', 'pet': 'cat', 'animal': 'rat'}


Create nested dictionary :
 
{'name': 'Dog', 'pet': False, 'age': 12, 'Flower': {'fruits': 'apple', 'kiwi': 'berry', 1: 'A'}}

Example 3: Create a dictionary with mixed data types

# a Dictionary of mixed data type

animal_dict = {'name':'Dog', 'pet':False,'age':12,'fruits':['apple','kiwi','berry'],1:'A'}

print("dictionary with different datatypes :\n ")

print(animal_dict)



#-------------------output-----------------------


dictionary with different datatypes :
 
{'name': 'Dog', 'pet': False, 'age': 12, 'fruits': ['apple', 'kiwi', 'berry'],1:'A'}

2. Create dictionary using dict()

The dict() is a constructor to create a dictionary. It does not return anything.

Syntax

dict(keyword_agrument*)

Keyword_argument*: Any number of (key:value,……..key:valueN) arguments can be passed separated by comma

Example :Creating an empty Dictionary

first_dict= dict()

print("Empty Dictionary: ")

print(first_dict)


#------output-------------

Empty Dictionary: 
{}

Example 2:Create Dictionary from list of tuples

#dictionary with data

first_dict =  dict({1: 'apple', 2: 'orange', 3:'kiwi'})

print("dictionary with data: ")

print(first_dict)


#dictionary from a list of tuples

first_dict = dict([(1, 'apple'), (2, 'orange'),(3,'kiwi')])

print("dictionary from a list of tuples: ")

print(first_dict)

Output

Dictionary with data: 

{1: 'apple', 2: 'orange', 3: 'kiwi'}


dictionary from list of tuples: 

{1: 'apple', 2: 'orange', 3: 'kiwi'}