Python Dictionary

Python dictionary is one of the types of collections used to store data in key and value pairs.

A dictionary can have int, string, boolean and list as data types.

The dictionary is ordered, changeable, and does not allow duplicate items.

The dictionary item with a duplicate key will always get overridden by the last item.

dictionaryOne = {
    "name" : "test",
    "id" : "1"
}
for key, value in dictionaryOne.items():
    print(key, value);


OUTPUT:
     name test
     id 1

Access Dictionary using Key

Below is the example to access value from dictionary using key:

dictionaryOne = {
    "name" : "test",
    "id" : "1"
}
print(dictionaryOne.get("name"));

OUTPUT:
     test

Methods in Dictionary

Below Dictionary methods is all about access, add, update and exist Dictionary item.

Please read the below code snippet with comments. This will help you get a better code understanding.

dictionaryOne = {
    "name" : "test",
    "id" : "1"
}

# get all keys from dictionary
print(dictionaryOne.keys());

# get all values from dictionary
print(dictionaryOne.values());

# get all items as key and value pair from dictionary
print(dictionaryOne.items());

#add or update an item in Disctionary
dictionaryOne.update({"age": "32"});
print(dictionaryOne.items());

#add or update an item in Disctionary
dictionaryOne["age"] = "30";
print(dictionaryOne.items());

# Check if particular key exist or not in Dictionary
if "name" in dictionaryOne:
    print("name exist in Dictionary!!");


OUTPUT:
dict_keys(['name', 'id'])
dict_values(['test', '1'])
dict_items([('name', 'test'), ('id', '1')])
dict_items([('name', 'test'), ('id', '1'), ('age', '32')])
dict_items([('name', 'test'), ('id', '1'), ('age', '30')])
name exist in Dictionary!!

Remove

Below is an example to remove item or all items from Dictionary.

dictionaryOne = {
    "name" : "test",
    "id" : "1",
    "age" : "30",
    "address" : "address 1"
}

# pop will remove item using key
print(dictionaryOne.pop("name"));

# remove itmem which inserted in last
print(dictionaryOne.popitem());

# del will remove item using key
del dictionaryOne["age"]
print(dictionaryOne);

# empty Dictionary
print(dictionaryOne.clear());


OUTPUT:
test
('address', 'address 1')
{'id': '1'}
None

Copy Dictionary

The following code will help us to create a complete copy of items from the existing Dictionary to the new one.

dictionaryOne = {
    "name" : "test",
    "id" : "1",
    "age" : "30",
    "address" : "address 1"
}

print(dictionaryOne);

# This is not a copy
# just a reference update to dictionaryOne
dictionarytwo = dictionaryOne;
print(dictionarytwo);

# Making a copy() of Dictionary
dictionaryThree = dictionaryOne.copy();
print(dictionaryThree);


OUTPUT:
{'name': 'test', 'id': '1', 'age': '30', 'address': 'address 1'}
{'name': 'test', 'id': '1', 'age': '30', 'address': 'address 1'}
{'name': 'test', 'id': '1', 'age': '30', 'address': 'address 1'}

Imran Khan

Specialist Master (Architect) with a passion for cutting-edge technologies like AEM (Adobe Experience Manager) and a proven track record of delivering high-quality software solutions.

  • Languages: Java, Python
  • Frameworks: J2EE, Spring, Struts 2.0, Hibernate
  • Web Technologies: React, HTML, CSS
  • Analytics: Adobe Analytics
  • Tools & Technologies: IntelliJ, JIRA

🌐 LinkedIn

📝 Blogs

📧 Imran Khan