Python: Dictionary [Data Type]

Dictionary in Python is a collection of key/value pairs. Dictionaries use keys(labels) to store item/value references, instead of numbered index.

Dictionary items are not ordered, and are not stored in any specific order.

big_box_dict = dict()

big_box_dict['first_item'] = "abc"
big_box_dict['second_item'] = "def"
big_box_dict['third_item'] = "ghi"
big_box_dict['fourth'] = "jkl"
big_box_dict['fifth'] = "mno"

print(type(big_box_dict))
print(len(big_box_dict))
print(big_box_dict)
print(dir(big_box_dict))
Python

Output:

<class 'dict'>

5

{'first_item': 'abc', 'second_item': 'def', 'third_item': 'ghi', 'fourth': 'jkl', 'fifth': 'mno'}

['__class__', '__class_getitem__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__ior__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__or__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__ror__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']
Plaintext

Or we can define and assign key/value as below-

product_info = {
    "name": "Sonic Wireless Headphones",
    "model": "SWH-30G21",
    "category": "Electronics > Audio > Headphones",
    "color": "Black",
    "weight": "0.5lbs",
    "battery_life": "30h",
    "speaker_size": "40mm",
    "price": "99.99",
}

print(type(product_info))
print(len(product_info))
print(product_info)
print(dir(product_info))
Python

Output:

<class 'dict'>

8

{'name': 'Sonic Wireless Headphones', 'model': 'SWH-30G21', 'category': 'Electronics > Audio > Headphones', 'color': 'Black', 'weight': '0.5lbs', 'battery_life': '30h', 'speaker_size': '40mm', 'price': '99.99'}

['__class__', '__class_getitem__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__ior__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__or__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__ror__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']
Plaintext

Loop through Dictionary

Case #1

product_info = {
    "name": "Sonic Wireless Headphones",
    "model": "SWH-30G21",
    "category": "Electronics > Audio > Headphones",
    "color": "Black",
    "weight": "0.5lbs",
    "battery_life": "30h",
    "speaker_size": "40mm",
    "price": "99.99",
}

for info_key in product_info:
    print(info_key)
Python

Output:

name
model
category
color
weight
battery_life
speaker_size
price
Plaintext

Case #2

product_info = {
    "name": "Sonic Wireless Headphones",
    "model": "SWH-30G21",
    "category": "Electronics > Audio > Headphones",
    "color": "Black",
    "weight": "0.5lbs",
    "battery_life": "30h",
    "speaker_size": "40mm",
    "price": "99.99",
}

for info_key in product_info:
    print(f"Key: {info_key} || Value: {product_info[info_key]}")
Python

Case #3

product_info = {
    "name": "Sonic Wireless Headphones",
    "model": "SWH-30G21",
    "category": "Electronics > Audio > Headphones",
    "color": "Black",
    "weight": "0.5lbs",
    "battery_life": "30h",
    "speaker_size": "40mm",
    "price": "99.99",
}

for info_key, value in product_info.items():
    print(f"Key: {info_key} || Value: {value}")
Python

Output:

Key: name || Value: Sonic Wireless Headphones
Key: model || Value: SWH-30G21
Key: category || Value: Electronics > Audio > Headphones
Key: color || Value: Black
Key: weight || Value: 0.5lbs
Key: battery_life || Value: 30h
Key: speaker_size || Value: 40mm
Key: price || Value: 99.99
Plaintext

Leave a Comment


The reCAPTCHA verification period has expired. Please reload the page.