Fundamentals 5 min read

Python Dictionary and Set Basics

This article introduces Python's built‑in dictionary and set types, explaining their characteristics, common operations, and providing example code to demonstrate creation, element access, and membership testing, while also highlighting key differences such as mutability of values and the use of hashable elements as keys.

Practical DevOps Architecture
Practical DevOps Architecture
Practical DevOps Architecture
Python Dictionary and Set Basics

Click the blue “DevOps Architecture Practice” to follow me.

Share to “Moments”; daily technical posts are pushed at 08:06 each morning.

1. Dictionary

Python's built‑in dictionary type provides associative array functionality using a hash table. The built‑in len() function returns the number of key‑value pairs. The del statement removes entries, and the dictionary offers methods such as clear, copy, get, items, keys, update, and values.

Example code:

x = {1: "one", 2: "two"}
x["first"] = "one"
x[("Delorme", "Hahashen", 2000)] = (1, 2, 3)
list(x.keys())
[1, 2, 'first', ('Delorme', 'Hahashen', 2000)]
x[1]
'one'
x[2]
'two'
x.get(1, "not available")
'one'
x.get(2, "not available")
'two'
x.get(3, "not available")
'not available'

Dictionary keys must be immutable types such as numbers, strings, or tuples, while values can be any object, including mutable types like lists or other dictionaries. Accessing a non‑existent key raises KeyError; using get() or providing a default value can avoid this exception.

2. Set

A set is defined as an unordered collection of distinct hashable elements, which can serve as dictionary keys.

Characteristics: (1) composed of different elements, (2) unordered, (3) elements must be immutable.

Python's set type stores unique objects without associated values, similar to the keys of a dictionary.

Example code:

x = set([1, 2, 3, 1, 3, 5])
x
{1, 2, 3, 5}
1 in x
True
4 in x
False

Calling set() on a sequence removes duplicate members; the in keyword checks membership.

----------------------end---------------------

Recommended reading:

Company requirement: Offline test environment DNS master‑slave setup

ActiveMQ vs RocketMQ comparison

Message middleware ActiveMQ + Zookeeper

Practical guide – deploying RocketMQ

Welcome to join technical exchange

If you found this useful, please click “Like”, thank you.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonData StructuresdictionarySet
Practical DevOps Architecture
Written by

Practical DevOps Architecture

Hands‑on DevOps operations using Docker, K8s, Jenkins, and Ansible—empowering ops professionals to grow together through sharing, discussion, knowledge consolidation, and continuous improvement.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.