Python Built-in Data Structures: Lists, Sets, Dictionaries, and Tuples
This article introduces Python's core built‑in data structures—lists, sets, dictionaries, and tuples—explaining their characteristics, typical use cases, and providing clear code examples for creation, modification, and access.
Lists are ordered, mutable collections that allow duplicate elements. They are suitable when element order matters and the collection needs to be modified, such as storing a sequence of numbers or names.
list = [1, 2, 3, 4, 5] # create a listlist.append(6) # add an elementprint(list) # output the listSets are unordered, mutable collections that automatically enforce uniqueness, making them ideal for deduplication and set operations like union, intersection, and difference.
set = {1, 2, 2} # duplicate values are removedset.add(4) # add an elementprint(set) # output: {1, 2, 4}Dictionaries store key‑value pairs; they are mutable and, since Python 3.6, preserve insertion order. Keys are unique, enabling fast lookup of values by key, useful for mappings such as user profiles or configuration data.
dict = {"name": "Alice", "age": 25}dict["age"] = 26 # modify a valueprint(dict["age"]) # output: 26Tuples are ordered, immutable collections that may contain duplicate elements. They are used when a fixed sequence of items should not change, such as coordinates or as keys in dictionaries.
tuple = (1, 2, 3) # immutable tupleprint(tuple) # output: (1, 2, 3)Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
